-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR implements support for opening immersive experiences directly as soon as the application is launched. We do this by adding a built-in extension which will activate a particular element in the page. We need to set the preference dom.vr.require-gesture to false so this script can launch immersive WebXR experiences. Media autoplay is also allowed in this mode. The information required to identify the element to activate is passed as parameters to the Intent: - open_in_immersive - open_in_immersive_parent_xpath - open_in_immersive_element_xpath - a target URL to open
- Loading branch information
1 parent
58c7851
commit f0f25fb
Showing
7 changed files
with
248 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
app/src/main/assets/extensions/wolvic_autowebxr/content.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
const LOGTAG = '[wolvic:autowebxr]'; | ||
const ENABLE_LOGS = true; | ||
const logDebug = (...args) => ENABLE_LOGS && console.log(LOGTAG, ...args); | ||
|
||
const PARENT_ELEMENT_XPATH_PARAMETER = 'wolvic-autowebxr-parentElementXPath'; | ||
const TARGET_ELEMENT_XPATH_PARAMETER = 'wolvic-autowebxr-targetElementXPath'; | ||
|
||
const IFRAME_READY_MSG = 'wolvic-autowebxr-iframeReady'; | ||
const TARGET_ELEMENT_MSG = 'wolvic-autowebxr-targetElement'; | ||
|
||
var parentElementXPath; | ||
var targetElementXPath; | ||
|
||
function getElementByXPath(document, xpath) { | ||
let result = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); | ||
return result.singleNodeValue; | ||
} | ||
|
||
// Limit the number of times that we can try to launch the experience, to avoid an infinite loop. | ||
var retryCounter = 0; | ||
const RETRY_LIMIT = 20; | ||
function retryAfterTimeout(code, delay) { | ||
if (retryCounter < RETRY_LIMIT) { | ||
retryCounter++; | ||
setTimeout(code, delay); | ||
} else { | ||
logDebug('Retry limit reached, will not try again'); | ||
} | ||
} | ||
|
||
function clickImmersiveElement() { | ||
// Check if the current URL has extra query parameters | ||
parentElementXPath = undefined; | ||
targetElementXPath = undefined; | ||
|
||
let url = document.URL; | ||
let params = new URLSearchParams(new URL(url).search); | ||
for (let [key, value] of params) { | ||
if (key === PARENT_ELEMENT_XPATH_PARAMETER) | ||
parentElementXPath = value; | ||
else if (key === TARGET_ELEMENT_XPATH_PARAMETER) | ||
targetElementXPath = value; | ||
} | ||
|
||
// We need at least the target element to click | ||
if (!targetElementXPath) | ||
return; | ||
|
||
logDebug('Preparing to open immersive WebXR; parentElementXPath: ' + parentElementXPath + ' ; targetElementXPath: ' + targetElementXPath); | ||
|
||
// The parent element is typically an iframe and, if it comes from a different origin, | ||
// we might not be able to access its contents directly. | ||
// If parentElementXPath is null, we will use the root to find the target element. | ||
|
||
var parent, parentDocument; | ||
if (parentElementXPath) { | ||
parent = getElementByXPath(document, parentElementXPath); | ||
if (!parent) { | ||
logDebug('Parent element not found, retrying'); | ||
retryAfterTimeout(clickImmersiveElement, 1000); | ||
return; | ||
} | ||
|
||
try { | ||
parentDocument = parent.contentDocument || parent.contentWindow.document; | ||
} catch (e) { | ||
logDebug('Parent iframe is from a different origin'); | ||
const iframeWindow = parent.contentWindow; | ||
|
||
const targetElementMsg = { | ||
action: TARGET_ELEMENT_MSG, | ||
targetElementXPath: targetElementXPath | ||
}; | ||
|
||
iframeWindow.postMessage(targetElementMsg, '*'); | ||
|
||
// The iframe might not be ready yet, so we set up a listener for the "iframe ready" message. | ||
const handleIframeReady = (event) => { | ||
if (event.source === iframeWindow && event.data === IFRAME_READY_MSG) { | ||
window.removeEventListener('message', handleIframeReady); | ||
iframeWindow.postMessage(targetElementMsg, '*'); | ||
} | ||
}; | ||
window.addEventListener('message', handleIframeReady); | ||
|
||
return; | ||
} | ||
} else { | ||
parent = window; | ||
parentDocument = document; | ||
} | ||
|
||
if (parentDocument.readyState !== 'complete') { | ||
logDebug('Parent is still loading'); | ||
parent.addEventListener('load', function() { | ||
logDebug('Parent has finished loading'); | ||
clickImmersiveElement(); | ||
}); | ||
return; | ||
} else { | ||
logDebug('Parent is loaded'); | ||
} | ||
|
||
let targetElement = getElementByXPath(parentDocument, targetElementXPath); | ||
|
||
if (targetElement) { | ||
logDebug('Target element found, calling click()'); | ||
targetElement.click(); | ||
targetElement.click(); | ||
} else { | ||
logDebug('Target element not found, we will try again'); | ||
retryAfterTimeout(clickImmersiveElement, 1000); | ||
} | ||
} | ||
|
||
function launchImmersiveFromIframe() { | ||
window.addEventListener('message', function(event) { | ||
if (event.data.action === TARGET_ELEMENT_MSG) { | ||
let targetElement = getElementByXPath(document, event.data.targetElementXPath); | ||
|
||
if (targetElement) { | ||
logDebug('Target element found in iframe, calling click()'); | ||
targetElement.click(); | ||
} else { | ||
logDebug('Target element not found in iframe, retrying'); | ||
retryAfterTimeout(function() { | ||
window.postMessage(event.data, '*'); | ||
}, 1000); | ||
} | ||
} | ||
}); | ||
|
||
window.parent.postMessage(IFRAME_READY_MSG, '*'); | ||
} | ||
|
||
// Main script execution | ||
if (window.top === window.self) { | ||
if (document.readyState === 'complete') { | ||
logDebug('Root document is completely ready'); | ||
clickImmersiveElement(); | ||
} else { | ||
logDebug('Root document is not ready yet'); | ||
window.addEventListener('load', clickImmersiveElement); | ||
} | ||
} else { | ||
if (document.readyState === 'complete') { | ||
logDebug('Iframe is completely ready'); | ||
launchImmersiveFromIframe(); | ||
} else { | ||
logDebug('Iframe is not ready yet'); | ||
window.addEventListener('load', launchImmersiveFromIframe); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/assets/extensions/wolvic_autowebxr/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"manifest_version": 2, | ||
"name": "Wolvic WebXR Automator", | ||
"version": "1.0", | ||
"description": "Enter WebXR Experiences Automatically", | ||
"browser_specific_settings": { | ||
"gecko": { | ||
"id": "wolvic-autowebxr@igalia.com" | ||
} | ||
}, | ||
"content_scripts": [ | ||
{ | ||
"matches": [ | ||
"*://*/*" | ||
], | ||
"js": [ | ||
"content.js" | ||
], | ||
"run_at": "document_idle", | ||
"all_frames": true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters