diff --git a/Safari Shared/Resources/content.js b/Safari Shared/Resources/content.js index 186ddb70..03aae1ec 100644 --- a/Safari Shared/Resources/content.js +++ b/Safari Shared/Resources/content.js @@ -89,9 +89,7 @@ function sendMessageToNativeApp(message) { // Receive from service-worker browser.runtime.onMessage.addListener((request, sender, sendResponse) => { - if ("popupDidAppear" in request && "id" in request) { - setTimeout(() => pollPopupStatus(request.id), 500); - } else if ("didTapExtensionButton" in request) { + if ("didTapExtensionButton" in request) { sendResponse({ host: window.location.host, favicon: getFavicon() }); } else if ("name" in request && request.name == "switchAccount") { sendMessageToNativeApp(request); @@ -140,10 +138,6 @@ function genId() { function didChangeVisibility() { if (document.pendingRequestsIds.size != 0 && document.visibilityState === 'visible') { - if (typeof document.stashedPopupId !== "undefined" && document.stashedPopupId > -1) { - pollPopupStatus(document.stashedPopupId); - document.stashedPopupId = -1; - } document.pendingRequestsIds.forEach(id => { const request = {id: id, subject: "getResponse", host: window.location.host, isMobile: isMobile}; browser.runtime.sendMessage(request).then(response => { @@ -158,18 +152,3 @@ function didChangeVisibility() { document.addEventListener('visibilitychange', didChangeVisibility); window.addEventListener('focus', didChangeVisibility); - -// MARK: - iOS extension popup - -function pollPopupStatus(id) { - if (document.visibilityState === 'visible') { - const request = {id: id, subject: "POPUP_CHECK", isMobile: isMobile}; - browser.runtime.sendMessage(request).then(response => { - if (typeof response !== "undefined" && "popupStillThere" in response) { - setTimeout(() => pollPopupStatus(id), 500); - } - }); - } else { - document.stashedPopupId = id; - } -} diff --git a/Safari Shared/Resources/service_worker.js b/Safari Shared/Resources/service_worker.js index 3e5146f4..eb803bae 100644 --- a/Safari Shared/Resources/service_worker.js +++ b/Safari Shared/Resources/service_worker.js @@ -1,20 +1,14 @@ // Copyright © 2023 Tokenary. All rights reserved. browser.runtime.onMessage.addListener((request, sender, sendResponse) => { - if (request.subject === "POPUP_CHECK") { - checkPopupStatus(request.id, sendResponse); - } else 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 (request.subject === "message-to-wallet") { + sendNativeMessage(request, sender, sendResponse); if (request.isMobile) { const name = request.message.name; - if (name != "switchEthereumChain" && name != "addEthereumChain" && name != "switchAccount") { - showPopupIfThereIsNoVisible(request.message); + if (name != "switchEthereumChain" && name != "addEthereumChain") { + mobileRedirectFor(request.message); } } - sendNativeMessage(request, sender, sendResponse); } else if (request.subject === "getResponse") { browser.runtime.sendNativeMessage("mac.tokenary.io", request).then(response => { if (typeof response !== "undefined") { @@ -139,127 +133,21 @@ browser.action.onClicked.addListener(tab => { return true; }); -// MARK: - iOS extension popup +// MARK: - mobile redirect -function checkPopupStatus(id, sendResponse) { - getCurrentPopupRequest().then(currentRequest => { - if (typeof currentRequest !== "undefined" && id == currentRequest.id) { - if (hasVisiblePopup()) { - sendResponse({ popupStillThere: true }); - } else { - didDismissPopup(); - } - } - }); -} - -function showPopupIfThereIsNoVisible(popupRequest) { - getCurrentPopupRequest().then(result => { - if (typeof result === "undefined" && !hasVisiblePopup()) { - storeCurrentPopupRequest(popupRequest); - browser.action.openPopup(); - } - }); -} - -function popupDidProceed(id) { - cleanupCurrentPopupRequestOnProceed(); -} - -function didDismissPopup() { - cleanupCurrentPopupRequestOnDismiss(); -} - -function cancelPopupRequest(request) { - const cancelResponse = { - id: request.id, - provider: request.provider, - name: request.name, - error: "canceled", - subject: "cancelRequest", - }; - browser.runtime.sendNativeMessage("mac.tokenary.io", cancelResponse).catch(() => {}); +function mobileRedirectFor(request) { + const query = encodeURIComponent(JSON.stringify(request)); browser.tabs.getCurrent((tab) => { if (tab) { - browser.tabs.sendMessage(tab.id, cancelResponse); - } - }); -} - -function didAppearPopup(tab, sendResponse) { - getCurrentPopupRequest().then(popupRequest => { - if (typeof popupRequest !== "undefined") { - sendResponse(popupRequest); - browser.tabs.sendMessage(tab.id, {popupDidAppear: true, id: popupRequest.id}); - } else { - const message = {didTapExtensionButton: true}; - browser.tabs.sendMessage(tab.id, message).then(response => { - if (typeof response !== "undefined" && typeof response.host !== "undefined") { - getLatestConfiguration(response.host).then(currentConfiguration => { - const latestConfigurations = currentConfiguration.latestConfigurations; - const switchAccountMessage = { - name: "switchAccount", - id: genId(), - provider: "unknown", - body: currentConfiguration, - host: response.host, - favicon: response.favicon - }; - sendResponse(switchAccountMessage); - storeCurrentPopupRequest(switchAccountMessage); - browser.tabs.sendMessage(tab.id, {popupDidAppear: true, id: switchAccountMessage.id}); - browser.tabs.sendMessage(tab.id, switchAccountMessage); - }); - } + browser.scripting.executeScript({ + target: { tabId: tab.id }, + func: (query) => { window.location.href = `https://tokenary.io/extension?query=${query}`; }, + args: [query] }); } }); } -function hasVisiblePopup() { - const popup = browser.extension.getViews({ type: 'popup' }); - if (popup.length === 0) { - return false; - } else if (popup.length > 0) { - return true; - } -} - -// MARK: - current popup storage - -function getCurrentPopupRequest() { - return new Promise((resolve) => { - browser.storage.session.get("currentPopup").then(result => { - if (typeof result !== "undefined") { - resolve(result["currentPopup"]); - } else { - resolve(); - } - }).catch(() => { - resolve(); - }); - }); -} - -function storeCurrentPopupRequest(request) { - browser.storage.session.set({ ["currentPopup"]: request }); -} - -function cleanupCurrentPopupRequestOnProceed() { - browser.storage.session.remove("currentPopup"); -} - -function cleanupCurrentPopupRequestOnDismiss() { - getCurrentPopupRequest().then(result => { - if (typeof result !== "undefined") { - cancelPopupRequest(result); - } - browser.storage.session.remove("currentPopup"); - }).catch(() => { - browser.storage.session.remove("currentPopup"); - }); -} - // MARK: - helpers function genId() { diff --git a/Safari iOS/Resources/manifest.json b/Safari iOS/Resources/manifest.json index f8092a42..8ea544de 100644 --- a/Safari iOS/Resources/manifest.json +++ b/Safari iOS/Resources/manifest.json @@ -26,7 +26,6 @@ }], "action": { - "default_popup": "popup.html", "default_icon": { "16": "images/toolbar-icon-16.png", "19": "images/toolbar-icon-19.png", diff --git a/Safari iOS/Resources/popup.css b/Safari iOS/Resources/popup.css deleted file mode 100644 index 9034d532..00000000 --- a/Safari iOS/Resources/popup.css +++ /dev/null @@ -1,38 +0,0 @@ -:root { - color-scheme: light dark; -} - -body { - width: 100%; - height: 100%; - font-family: system-ui; - text-align: center; - align-content: center; - justify-content: center; - min-width: 200pt; - margin: 0; - margin-top: 23pt; - margin-bottom: 23pt; -} - -#tokenary-button { - font-size: 21pt; - font-weight: bold; - color: white; - background-color: #0093FF; - width: 88%; - min-height: 100pt; - border: none; - border-radius: min(17vw, 17vh); - -webkit-touch-callout: none; - -webkit-user-select: none; - user-select: none; - padding: 8pt; - line-height: 1.2em; - text-align: center; - margin: 0; -} - -@media (prefers-color-scheme: dark) { - -} diff --git a/Safari iOS/Resources/popup.html b/Safari iOS/Resources/popup.html deleted file mode 100644 index ea37b5b8..00000000 --- a/Safari iOS/Resources/popup.html +++ /dev/null @@ -1,11 +0,0 @@ - - -
- - - - - - - - diff --git a/Safari iOS/Resources/popup.js b/Safari iOS/Resources/popup.js deleted file mode 100644 index 9b9dedd6..00000000 --- a/Safari iOS/Resources/popup.js +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright © 2023 Tokenary. All rights reserved. - -const button = document.getElementById('tokenary-button'); -var message = {}; - -browser.tabs.getCurrent(tab => { - browser.runtime.sendMessage({subject: 'POPUP_APPEARED', tab: tab, isMobile: true}).then((response) => { - message = response; - setupButton(); - }); -}); - -button.addEventListener('click', () => { - browser.runtime.sendMessage({subject: 'POPUP_DID_PROCEED', id: message.id, isMobile: true}); - const query = encodeURIComponent(JSON.stringify(message)); - browser.tabs.getCurrent((tab) => { - if (tab) { - browser.scripting.executeScript({ - target: { tabId: tab.id }, - func: (query) => { window.location.href = `https://tokenary.io/extension?query=${query}`; }, - args: [query] - }); - } - }); - return true; -}); - -function setupButton() { - var title = "open the app"; - switch (message.name) { - case "signPersonalMessage": - case "signMessage": - case "signTypedMessage": - title = "sign message\nin the app"; - break; - case "signTransaction": - case "signAndSendTransactions": - case "signAllTransactions": - case "signAndSendTransaction": - title = "approve transaction\nin the app"; - break; - case "requestAccounts": - case "signIn": - case "connect": - title = "connect"; - break; - case "switchAccount": - const latestConfigurations = message.body.latestConfigurations; - if (Array.isArray(latestConfigurations) && latestConfigurations.length) { - title = "switch\naccount"; - } else { - title = "connect\nwallet"; - } - break; - } - - button.innerText = title; -} diff --git a/Tokenary.xcodeproj/project.pbxproj b/Tokenary.xcodeproj/project.pbxproj index 8d769cac..956b972b 100644 --- a/Tokenary.xcodeproj/project.pbxproj +++ b/Tokenary.xcodeproj/project.pbxproj @@ -102,9 +102,6 @@ 2C773F63274523DC007B04E7 /* ResponseToExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C773F61274523DC007B04E7 /* ResponseToExtension.swift */; }; 2C78F8282683BDCC00C10670 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C78F8272683BDCC00C10670 /* Alert.swift */; }; 2C797E7E267BB88800F2CE2D /* WelcomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C797E7D267BB88800F2CE2D /* WelcomeViewController.swift */; }; - 2C8155682ADFC7A700F4559B /* popup.css in Resources */ = {isa = PBXBuildFile; fileRef = 2C8155662ADFC7A700F4559B /* popup.css */; }; - 2C81556B2ADFC7B900F4559B /* popup.js in Resources */ = {isa = PBXBuildFile; fileRef = 2C8155692ADFC7B900F4559B /* popup.js */; }; - 2C81556E2ADFC7C400F4559B /* popup.html in Resources */ = {isa = PBXBuildFile; fileRef = 2C81556C2ADFC7C400F4559B /* popup.html */; }; 2C8944C52AEAFF97006A711F /* FixedWidthInteger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8944C42AEAFF97006A711F /* FixedWidthInteger.swift */; }; 2C8944C62AEAFF97006A711F /* FixedWidthInteger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8944C42AEAFF97006A711F /* FixedWidthInteger.swift */; }; 2C8944CB2AEB0C10006A711F /* EthereumRPC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8944CA2AEB0C10006A711F /* EthereumRPC.swift */; }; @@ -380,9 +377,6 @@ 2C773F61274523DC007B04E7 /* ResponseToExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseToExtension.swift; sourceTree = "