Skip to content

Commit

Permalink
Update the CWS script to work on the new store.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlex94 committed Feb 19, 2024
1 parent 9c74686 commit fc032e1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 91 deletions.
127 changes: 38 additions & 89 deletions waterfox/browser/components/addonstores/extension/cws.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}
}

Expand All @@ -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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit fc032e1

Please sign in to comment.