From fc032e1f774db2552c31b45e5e373f2de40301bc Mon Sep 17 00:00:00 2001 From: Alex Kontos Date: Wed, 14 Feb 2024 14:47:14 +0000 Subject: [PATCH] Update the CWS script to work on the new store. --- .../components/addonstores/extension/cws.js | 127 ++++++------------ .../addonstores/extension/manifest.json | 4 +- 2 files changed, 40 insertions(+), 91 deletions(-) diff --git a/waterfox/browser/components/addonstores/extension/cws.js b/waterfox/browser/components/addonstores/extension/cws.js index 74c6578525128..ff95b4bb1b97f 100644 --- a/waterfox/browser/components/addonstores/extension/cws.js +++ b/waterfox/browser/components/addonstores/extension/cws.js @@ -6,17 +6,16 @@ "use-strict"; function uninit() { - removeStyleSheet(); - removeInstallClickHandlers(document.body); + updateInstallClickHandlers(document.body, false); unwatchAddingInstallHandlers(); } function init() { - addStyleSheet(); (function initInstallHandlers() { if (document.body) { - addInstallClickHandlers(document.body); + updateInstallClickHandlers(document.body, true); watchForAddingInstallHandlers(); + replaceButtonText(); return; } window.requestAnimationFrame(initInstallHandlers); @@ -26,54 +25,36 @@ function init() { let gObserver; init(); -function addStyleSheet() { - const styleSheet = document.createElement("style"); - styleSheet.setAttribute("id", "wf-addons-store-style"); - styleSheet.textContent = ` - div[role=dialog][aria-labelledby="promo-header"] - { - visibility: hidden; - } - div[role=button][aria-label*="CHROME"], - div[role=button][aria-label*="Chrome"] - { - background-color: rgb(124, 191, 54); - background-image: linear-gradient(to bottom, rgb(124, 191, 54), rgb(101, 173, 40)); - border-color:rgb(78, 155, 25); - } - div[role=button][aria-label*="CHROME"] .webstore-test-button-label, - div[role=button][aria-label*="Chrome"] .webstore-test-button-label - { - font-size: 0; - } - div[role=button][aria-label*="CHROME"] .webstore-test-button-label::before, - div[role=button][aria-label*="Chrome"] .webstore-test-button-label::before - { - display: flex; - content: "Add To Waterfox"; - justify-content: center; - align-items: center; - font-size: 14px; - } - /* targeting download div */ - body > div:last-of-type > div:nth-of-type(2), - /* alt target download div */ - .h-Yb-wa.Yb-wa - { - display: none; - } - `; - - document.documentElement.insertBefore( - styleSheet, - document.documentElement.firstChild - ); +function hideElements() { + const elementsToHide = Array.from(document.querySelectorAll('[aria-labelledby="promo-header"], [aria-label="info"]')); + + for (const element of elementsToHide) { + element.style.display = 'none'; + } } -function removeStyleSheet() { - const styleSheet = document.getElementById("wf-addons-store-style"); - if (styleSheet) { - styleSheet.remove(styleSheet); +function replaceButtonText() { + const buttons = Array.from(document.querySelectorAll('button')).filter(button => button.textContent.includes('Add to Chrome')); + + for (const button of buttons) { + button.textContent = button.textContent.replace('Add to Chrome', 'Add to Waterfox'); + button.style.color = 'white'; // Add this line + } +} + +function updateInstallClickHandlers(node, addHandlers) { + if (node.nodeType === Node.ELEMENT_NODE) { + const buttons = Array.from(node.querySelectorAll('button')).filter(button => button.textContent.includes('Add to Chrome')); + + for (const button of buttons) { + if (addHandlers) { + button.removeAttribute('disabled'); + button.addEventListener("click", handleInstall, true); + } else { + button.setAttribute('disabled', ''); + button.removeEventListener("click", handleInstall, true); + } + } } } @@ -98,61 +79,29 @@ function handleInstall(e) { e.preventDefault(); e.stopPropagation(); - // start figure out id - // Thanks to @Rob--W the id is accurately obtained: "It is the first 32 characters of the public key's sha256 hash, with the 0-9a-f replaced with a-p" - const extIdPatt = /[^a-p]([a-p]{32})[^a-p]/i; - const extId = parentNodeUntil(e.target, 100, node => { - if (node.nodeType === Node.ELEMENT_NODE) { - const [, extId] = extIdPatt.exec(node.innerHTML) || []; - console.log("extId:", extId); - return extId; - } - }); + // Extract the extension ID from the URL of the page + const urlParts = window.location.pathname.split('/'); + const extId = urlParts[urlParts.length - 1]; + if (!extId) { alert( - "Addon Stores Compatibility enecountered an error. Failed to determine extension ID." + "Addon Stores Compatibility encountered an error. Failed to determine extension ID." ); } else { let downloadURL = buildDownloadURL(extId); - // Send downloadURL to background script browser.runtime.sendMessage({ downloadURL, }); } } -function addInstallClickHandlers(node) { - if (node.nodeType === Node.ELEMENT_NODE) { - const buttons = [ - ...node.querySelectorAll('div[role=button][aria-label*="Chrome"]'), - ...node.querySelectorAll('div[role=button][aria-label*="CHROME"]'), - ]; - - for (const button of buttons) { - button.addEventListener("click", handleInstall, true); - } - } -} - -function removeInstallClickHandlers(node) { - if (node.nodeType === Node.ELEMENT_NODE) { - const buttons = [ - ...node.querySelectorAll('div[role=button][aria-label*="Chrome"]'), - ...node.querySelectorAll('div[role=button][aria-label*="CHROME"]'), - ]; - - for (const button of buttons) { - button.removeEventListener("click", handleInstall, true); - } - } -} - function watchForAddingInstallHandlers() { gObserver = new MutationObserver(mutations => { for (const mutation of mutations) { if (mutation.type === "childList") { for (const node of mutation.addedNodes) { - addInstallClickHandlers(node); + updateInstallClickHandlers(node, true); + hideElements(); } } } diff --git a/waterfox/browser/components/addonstores/extension/manifest.json b/waterfox/browser/components/addonstores/extension/manifest.json index 4db0b7f3c780c..2a4d702771bde 100644 --- a/waterfox/browser/components/addonstores/extension/manifest.json +++ b/waterfox/browser/components/addonstores/extension/manifest.json @@ -15,8 +15,8 @@ "content_scripts": [ { "matches": [ - "http://chrome.google.com/webstore*", - "https://chrome.google.com/webstore*" + "http://chromewebstore.google.com/*", + "https://chromewebstore.google.com/*" ], "js": ["cws.js"], "run_at": "document_start",