From 03f6e93f2380a87cf6002e57e996799fd8f9a2ca Mon Sep 17 00:00:00 2001 From: ivan grachev Date: Wed, 13 Dec 2023 15:21:05 +0300 Subject: [PATCH] pass isMobile value to service worker --- Safari Shared/Resources/content.js | 7 ++++--- Safari Shared/Resources/service_worker.js | 13 +++++-------- Safari iOS/Resources/manifest.json | 2 +- Safari iOS/Resources/popup.js | 4 ++-- Safari macOS/Resources/manifest.json | 2 +- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Safari Shared/Resources/content.js b/Safari Shared/Resources/content.js index 77b7238e..8bde7fbf 100644 --- a/Safari Shared/Resources/content.js +++ b/Safari Shared/Resources/content.js @@ -61,7 +61,7 @@ if (shouldInjectProvider()) { } function getLatestConfiguration() { - const request = {subject: "getLatestConfiguration", host: window.location.host}; + const request = {subject: "getLatestConfiguration", host: window.location.host, isMobile: isMobile}; browser.runtime.sendMessage(request).then((response) => { if (typeof response === "undefined") { return; } const id = genId(); @@ -81,7 +81,7 @@ function sendMessageToNativeApp(message) { message.favicon = getFavicon(); message.host = window.location.host; document.pendingRequestsIds.add(message.id); - browser.runtime.sendMessage({ subject: "message-to-wallet", message: message, host: window.location.host }).then((response) => { + browser.runtime.sendMessage({ subject: "message-to-wallet", message: message, host: window.location.host, isMobile: isMobile }).then((response) => { if (typeof response === "undefined") { return; } sendToInpage(response, message.id); }); @@ -107,6 +107,7 @@ window.addEventListener("message", event => { } else if (event.data.subject == "disconnect") { const disconnectRequest = event.data; disconnectRequest.host = window.location.host; + disconnectRequest.isMobile = isMobile; browser.runtime.sendMessage(disconnectRequest); } } @@ -138,7 +139,7 @@ function genId() { function didChangeVisibility() { if (document.pendingRequestsIds.size != 0 && document.visibilityState === 'visible') { document.pendingRequestsIds.forEach(id => { - const request = {id: id, subject: "getResponse", host: window.location.host}; + const request = {id: id, subject: "getResponse", host: window.location.host, isMobile: isMobile}; browser.runtime.sendMessage(request).then(response => { if (typeof response !== "undefined") { sendToInpage(response, id); diff --git a/Safari Shared/Resources/service_worker.js b/Safari Shared/Resources/service_worker.js index b785799d..e4350fcb 100644 --- a/Safari Shared/Resources/service_worker.js +++ b/Safari Shared/Resources/service_worker.js @@ -1,15 +1,12 @@ // Copyright © 2022 Tokenary. All rights reserved. -const isMobile = true; // TODO: setup from platform-specific content script - browser.runtime.onMessage.addListener((request, sender, sendResponse) => { - if (request.subject === "POPUP_PING") { - } else if (request.subject === "POPUP_DID_PROCEED") { + if (request.subject === "POPUP_DID_PROCEED") { popupDidProceed(request.id); } else if (request.subject === "POPUP_APPEARED") { didAppearPopup(request.tab, sendResponse); } else if (request.subject === "message-to-wallet") { - if (isMobile) { + if (request.isMobile) { const name = request.message.name; if (name != "switchEthereumChain" && name != "addEthereumChain" && name != "switchAccount") { addToPopupQueue(request.message); @@ -21,7 +18,7 @@ browser.runtime.onMessage.addListener((request, sender, sendResponse) => { if (typeof response !== "undefined") { sendResponse(response); storeConfigurationIfNeeded(request.host, response); - waitAndShowNextPopupIfNeeded(isMobile); + waitAndShowNextPopupIfNeeded(request.isMobile); } }).catch(() => {}); } else if (request.subject === "getLatestConfiguration") { @@ -57,7 +54,7 @@ function sendNativeMessage(request, sender, sendResponse) { if (typeof response !== "undefined") { sendResponse(response); storeConfigurationIfNeeded(request.host, response); - waitAndShowNextPopupIfNeeded(isMobile); + waitAndShowNextPopupIfNeeded(request.isMobile); } }).catch(() => {}); } @@ -150,7 +147,7 @@ function genId() { function waitAndShowNextPopupIfNeeded(isMobile) { if (isMobile) { - setTimeout(processPopupQueue, 420); + setTimeout(processPopupQueue, 420); // TODO: hmmm } } diff --git a/Safari iOS/Resources/manifest.json b/Safari iOS/Resources/manifest.json index 3938149f..f8092a42 100644 --- a/Safari iOS/Resources/manifest.json +++ b/Safari iOS/Resources/manifest.json @@ -22,7 +22,7 @@ "matches": [ "file://*/*", "http://*/*", "https://*/*" ], "run_at": "document_start", "all_frames": false, - "js": [ "content.js", "ios_specific_content.js" ] + "js": [ "ios_specific_content.js", "content.js" ] }], "action": { diff --git a/Safari iOS/Resources/popup.js b/Safari iOS/Resources/popup.js index 136068cd..a74fedaa 100644 --- a/Safari iOS/Resources/popup.js +++ b/Safari iOS/Resources/popup.js @@ -4,7 +4,7 @@ const button = document.getElementById('tokenary-button'); var message = {}; browser.tabs.getCurrent(tab => { - browser.runtime.sendMessage({subject: 'POPUP_APPEARED', tab: tab}).then((response) => { + browser.runtime.sendMessage({subject: 'POPUP_APPEARED', tab: tab, isMobile: true}).then((response) => { message = response; setupButton(); }); @@ -21,7 +21,7 @@ button.addEventListener('click', () => { }); } }); - browser.runtime.sendMessage({subject: 'POPUP_DID_PROCEED', id: message.id}); + browser.runtime.sendMessage({subject: 'POPUP_DID_PROCEED', id: message.id, isMobile: true}); setTimeout(window.close, 437); return true; }); diff --git a/Safari macOS/Resources/manifest.json b/Safari macOS/Resources/manifest.json index 2af6efb9..ed1769d2 100644 --- a/Safari macOS/Resources/manifest.json +++ b/Safari macOS/Resources/manifest.json @@ -22,7 +22,7 @@ "matches": [ "file://*/*", "http://*/*", "https://*/*" ], "run_at": "document_start", "all_frames": false, - "js": [ "content.js", "macos_specific_content.js" ] + "js": [ "macos_specific_content.js", "content.js" ] }], "action": {