From e26531f832b2095c58a5cb69d51779689358b873 Mon Sep 17 00:00:00 2001 From: NeverDecaf Date: Sat, 4 Dec 2021 02:34:02 -0500 Subject: [PATCH] replace tabs with spaces --- src/manifest.json | 2 +- src/scripts/background.js | 2 +- src/scripts/inject.js | 148 ++++++++++++++-------------- src/scripts/options.js | 28 +++--- src/scripts/util.js | 198 +++++++++++++++++++------------------- 5 files changed, 189 insertions(+), 189 deletions(-) diff --git a/src/manifest.json b/src/manifest.json index 2aff88b..b462486 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -27,7 +27,7 @@ "matches": ["https://chrome.google.com/webstore/*","https://addons.opera.com/*extensions/details/*", "https://microsoftedge.microsoft.com/addons/*"], "js": ["scripts/inject.js"], "run_at": "document_end" - }], + }], "background": { "scripts": ["scripts/util.js", "scripts/background.js"], "persistent": false diff --git a/src/scripts/background.js b/src/scripts/background.js index d2aca81..95dfa68 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -16,7 +16,7 @@ function handleContextClick(info, tab) { chrome.tabs.sendMessage(tab.id, { action: "install" }); - else if (info.menuItemId == 'cws') + else if (info.menuItemId == 'cws') chrome.tabs.create({ url: 'https://chrome.google.com/webstore/' }); diff --git a/src/scripts/inject.js b/src/scripts/inject.js index 59fd861..7faa54b 100644 --- a/src/scripts/inject.js +++ b/src/scripts/inject.js @@ -13,16 +13,16 @@ function getExtensionId(url) { } function buildExtensionUrl(extensionId) { - if (is_cws.test(window.location.href)) { - var chromeVersion = /Chrome\/([0-9.]+)/.exec(navigator.userAgent)[1]; - return 'https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&prodversion=' + chromeVersion + '&x=id%3D' + extensionId + '%26installsource%3Dondemand%26uc'; - } - if (is_ows.test(window.location.href)) { - return 'https://addons.opera.com/extensions/download/' + extensionId + '/' - } - if (is_ews.test(window.location.href)) { - return 'https://edge.microsoft.com/extensionwebstorebase/v1/crx?response=redirect&x=id%3D' + extensionId + '%26installsource%3Dondemand%26uc' - } + if (is_cws.test(window.location.href)) { + var chromeVersion = /Chrome\/([0-9.]+)/.exec(navigator.userAgent)[1]; + return 'https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&prodversion=' + chromeVersion + '&x=id%3D' + extensionId + '%26installsource%3Dondemand%26uc'; + } + if (is_ows.test(window.location.href)) { + return 'https://addons.opera.com/extensions/download/' + extensionId + '/' + } + if (is_ews.test(window.location.href)) { + return 'https://edge.microsoft.com/extensionwebstorebase/v1/crx?response=redirect&x=id%3D' + extensionId + '%26installsource%3Dondemand%26uc' + } } function createButton(newParent) { @@ -54,7 +54,7 @@ function createButton(newParent) { }); newParent.innerHTML = ""; newParent.appendChild(button_div); - dlBtn = button_div + dlBtn = button_div } var modifyButtonObserver = new MutationObserver(function (mutations, observer) { mutations.forEach(function (mutation) { @@ -76,79 +76,79 @@ attachMainObserver = new MutationObserver(function (mutations, observer) { observer.disconnect(); }); if (is_ews.test(window.location.href)) { - new MutationObserver(function (mutations, observer) { + new MutationObserver(function (mutations, observer) { mutations.forEach(function (mutation) { - if (is_ews_addon_page.test(window.location.href)) { - let btn = mutation.target.querySelector('button[disabled]') - if (btn) { - btn.classList.remove(btn.className.split(' ').sort().reverse()[0]) - btn.removeAttribute('disabled') - btn.addEventListener('click',() => { - // normal methods fail because microsoft's official web store redirects you from HTTPS to an insecure HTTP url. - // instead use chrome.tabs to open the url in a new tab. - chrome.runtime.sendMessage({ - newTabUrl: buildExtensionUrl(getExtensionId(window.location.href)) - }); - }) - } - } + if (is_ews_addon_page.test(window.location.href)) { + let btn = mutation.target.querySelector('button[disabled]') + if (btn) { + btn.classList.remove(btn.className.split(' ').sort().reverse()[0]) + btn.removeAttribute('disabled') + btn.addEventListener('click',() => { + // normal methods fail because microsoft's official web store redirects you from HTTPS to an insecure HTTP url. + // instead use chrome.tabs to open the url in a new tab. + chrome.runtime.sendMessage({ + newTabUrl: buildExtensionUrl(getExtensionId(window.location.href)) + }); + }) + } + } + }); + }).observe(document.body, { + childList: true, + subtree: true }); - }).observe(document.body, { - childList: true, - subtree: true - }); } if (is_cws.test(window.location.href)) { - attachMainObserver.observe(document.body, { - childList: true - }); + attachMainObserver.observe(document.body, { + childList: true + }); } if (is_ows.test(window.location.href)) { - let installDiv = document.body.querySelector('.sidebar .get-opera') - let sidebar = installDiv.parentElement - let wrapper = document.createElement('div') - wrapper.classList.add('wrapper-install') - dlBtn = document.createElement('a') - dlBtn.classList.add('btn-install') - dlBtn.classList.add('btn-with-plus') - dlBtn.innerHTML = chrome.i18n.getMessage("webstore_addButton") - sidebar.replaceChild(wrapper, installDiv) - wrapper.appendChild(dlBtn) - let url = buildExtensionUrl(getExtensionId(window.location.href)) - function fetchExt() { - let filename = 'ext.crx' - fetch(url) - .then( r => { - r.headers.forEach(h => { - let v = /filename=([^ ]+)/.exec(h) - if (v) { - filename = v[1] - return - } - }) - return r.blob() - }) - .then( blob => { - // set mime type to prevent automatic install; reference: https://stackoverflow.com/questions/57834691/how-to-serve-crx-file-in-a-way-that-is-not-automatically-installed - blob = blob.slice(0, blob.size, "application/zip") - const blobURL = window.URL.createObjectURL(blob); - dlBtn.href = blobURL; - dlBtn.download = filename - dlBtn.click() - }) - } - dlBtn.addEventListener('click', fetchExt, {once:true}) + let installDiv = document.body.querySelector('.sidebar .get-opera') + let sidebar = installDiv.parentElement + let wrapper = document.createElement('div') + wrapper.classList.add('wrapper-install') + dlBtn = document.createElement('a') + dlBtn.classList.add('btn-install') + dlBtn.classList.add('btn-with-plus') + dlBtn.innerHTML = chrome.i18n.getMessage("webstore_addButton") + sidebar.replaceChild(wrapper, installDiv) + wrapper.appendChild(dlBtn) + let url = buildExtensionUrl(getExtensionId(window.location.href)) + function fetchExt() { + let filename = 'ext.crx' + fetch(url) + .then( r => { + r.headers.forEach(h => { + let v = /filename=([^ ]+)/.exec(h) + if (v) { + filename = v[1] + return + } + }) + return r.blob() + }) + .then( blob => { + // set mime type to prevent automatic install; reference: https://stackoverflow.com/questions/57834691/how-to-serve-crx-file-in-a-way-that-is-not-automatically-installed + blob = blob.slice(0, blob.size, "application/zip") + const blobURL = window.URL.createObjectURL(blob); + dlBtn.href = blobURL; + dlBtn.download = filename + dlBtn.click() + }) + } + dlBtn.addEventListener('click', fetchExt, {once:true}) } window.onload = () => { chrome.runtime.onMessage.addListener(request => { if (request.action === "install") { - console.log('opening extension URL:',buildExtensionUrl(getExtensionId(window.location.href))) - if (is_ows.test(window.location.href)) { - dlBtn.click() - } - else if (is_cws.test(window.location.href) || is_ews.test(window.location.href)) { - window.open(buildExtensionUrl(getExtensionId(window.location.href))); - } + console.log('opening extension URL:',buildExtensionUrl(getExtensionId(window.location.href))) + if (is_ows.test(window.location.href)) { + dlBtn.click() + } + else if (is_cws.test(window.location.href) || is_ews.test(window.location.href)) { + window.open(buildExtensionUrl(getExtensionId(window.location.href))); + } } }); }; diff --git a/src/scripts/options.js b/src/scripts/options.js index df316da..c5b26e1 100644 --- a/src/scripts/options.js +++ b/src/scripts/options.js @@ -18,10 +18,10 @@ function load_options() { input = document.createElement('input'); input.setAttribute('type', 'checkbox'); input.setAttribute('id', ex.id); - if (Array.from(store_extensions.keys()).some(x => x.test(ex.updateUrl) && store_extensions.get(x).ignore)) { - input.checked = true - input.disabled = true - } + if (Array.from(store_extensions.keys()).some(x => x.test(ex.updateUrl) && store_extensions.get(x).ignore)) { + input.checked = true + input.disabled = true + } img.setAttribute('alt', ex.name); if (ex.icons) img.setAttribute('src', 'chrome://extension-icon/' + ex.id + '/' + ex.icons[0].size + '/0'); @@ -36,14 +36,14 @@ function load_options() { default_options[ex.id] = false; }); document.getElementById('import_export_list').value = e.map(ex => { - for (const [re, updaterOptions] of store_extensions) { - if (re.test(ex.updateUrl)) { - if (!updaterOptions.ignore) - return ex.name+'|'+ex.id - else - return ex.name - } - } + for (const [re, updaterOptions] of store_extensions) { + if (re.test(ex.updateUrl)) { + if (!updaterOptions.ignore) + return ex.name+'|'+ex.id + else + return ex.name + } + } return ex.name+'|'+ex.id+'|'+ex.updateUrl }).join('\r\n'); document.getElementById('import_all_button').onclick = () => { @@ -71,8 +71,8 @@ function load_options() { for (const [setting, value] of Object.entries(items)) { let node = document.getElementById(setting); if (node.type == 'checkbox') { - if (!node.checked) - node.checked = value; + if (!node.checked) + node.checked = value; node.addEventListener("change", e => { const checked = e.target.checked; chrome.storage.sync.set({ diff --git a/src/scripts/util.js b/src/scripts/util.js index 7e0f3b4..8872460 100644 --- a/src/scripts/util.js +++ b/src/scripts/util.js @@ -2,21 +2,21 @@ const chromeVersion = /Chrome\/([0-9.]+)/.exec(navigator.userAgent)[1]; const store_extensions = new Map() const googleUpdateUrl = 'https://clients2.google.com/service/update2/crx'; store_extensions.set(/clients2\.google\.com\/service\/update2\/crx/, { - baseUrl: 'https://clients2.google.com/service/update2/crx?response=updatecheck&acceptformat=crx2,crx3&prodversion=', - name: 'CWS Extensions', + baseUrl: 'https://clients2.google.com/service/update2/crx?response=updatecheck&acceptformat=crx2,crx3&prodversion=', + name: 'CWS Extensions', }) // edge requires &v= for each extension or else it returns empty results store_extensions.set(/edge\.microsoft\.com\/extensionwebstorebase\/v1\/crx/, { - baseUrl: 'https://edge.microsoft.com/extensionwebstorebase/v1/crx?os=win&arch=x64&os_arch=x86_64&nacl_arch=x86-64&prod=edgecrx&prodchannel=&lang=en-US&acceptformat=crx3&prodversion=', - name: 'Edge Extensions', - ignore: true, + baseUrl: 'https://edge.microsoft.com/extensionwebstorebase/v1/crx?os=win&arch=x64&os_arch=x86_64&nacl_arch=x86-64&prod=edgecrx&prodchannel=&lang=en-US&acceptformat=crx3&prodversion=', + name: 'Edge Extensions', + ignore: true, }) // opera requires an opera UA or else this request will return 404 store_extensions.set(/extension-updates\.opera\.com\/api\/omaha\/update/, { - baseUrl: 'https://extension-updates.opera.com/api/omaha/update/?os=win&arch=x64&os_arch=x86_64&nacl_arch=x86-64&prod=chromiumcrx&prodchannel=Stable&lang=en-US&acceptformat=crx3&prodversion=', - name: 'Opera Extensions', - userAgent: 'foobar', - ignore: true, + baseUrl: 'https://extension-updates.opera.com/api/omaha/update/?os=win&arch=x64&os_arch=x86_64&nacl_arch=x86-64&prod=chromiumcrx&prodchannel=Stable&lang=en-US&acceptformat=crx3&prodversion=', + name: 'Opera Extensions', + userAgent: 'foobar', + ignore: true, }) function version_is_newer(current, available) { @@ -69,17 +69,17 @@ function checkForUpdates(update_callback = null, failure_callback = null, comple let updateUrl = 'https://clients2.google.com/service/update2/crx?response=updatecheck&acceptformat=crx2,crx3&prodversion=' + chromeVersion; let installed_versions = {}; let updateUrls = []; - Array.from(store_extensions.values()).forEach(x => delete x.updateUrl) + Array.from(store_extensions.values()).forEach(x => delete x.updateUrl) e.forEach(function (ex) { if (ex.updateUrl && !settings[ex.id]) { - let is_from_store = false - for (const [re, updaterOptions] of store_extensions) { - if (re.test(ex.updateUrl)) { - is_from_store = true - updaterOptions.updateUrl = updaterOptions.updateUrl || (updaterOptions.baseUrl + chromeVersion) - updaterOptions.updateUrl += '&x=id%3D' + ex.id + '%26uc'; - } - } + let is_from_store = false + for (const [re, updaterOptions] of store_extensions) { + if (re.test(ex.updateUrl)) { + is_from_store = true + updaterOptions.updateUrl = updaterOptions.updateUrl || (updaterOptions.baseUrl + chromeVersion) + updaterOptions.updateUrl += '&x=id%3D' + ex.id + '%26uc'; + } + } if (!is_from_store && settings.check_external_apps) { updateUrls.push({ 'url': ex.updateUrl, @@ -91,90 +91,90 @@ function checkForUpdates(update_callback = null, failure_callback = null, comple } }); if (settings.check_store_apps) - for (const [re, updaterOptions] of store_extensions) { - if (!updaterOptions.ignore) - updateUrls.push({ - 'url': updaterOptions.updateUrl, - 'name': updaterOptions.name - }); - } - function update_extension(ext_url, ext_id, ext_name) { - let is_webstore = Array.from(store_extensions.keys()).some(x => x.test(ext_url)) - return new Promise((resolve, reject) => { - fetch(ext_url) - .then(r => { - if (r.status != 200) { - return Promise.reject() - } - else - return r.text() - }) - .then(txt => new window.DOMParser().parseFromString(txt, "text/xml")) - .then(data => { - let updates = data.getElementsByTagName('app'); - let updateCount = 0; - for (let i = 0; i < updates.length; i++) { - if (updateCheck = updates[i].querySelector("*")) { - let updatever = updateCheck.getAttribute('version'); - let appid = updates[i].getAttribute('appid'); - let updatestatus = updateCheck.getAttribute('status'); - if ((updatestatus == 'ok' || !is_webstore) && updatever && installed_versions[appid] !== undefined && version_is_newer(installed_versions[appid].version, updatever)) { - updateCount++; - if (update_callback) - update_callback(updateCheck, installed_versions, appid, updatever, is_webstore); - if (appid in stored_values["removed_extensions"]) { - delete stored_values['removed_extensions'][appid]; - chrome.storage.sync.set({ - 'removed_extensions': stored_values['removed_extensions'] - }); - } - } - if (failure_callback && updatestatus == 'noupdate' && !(appid in stored_values["removed_extensions"])) - failure_callback(true, installed_versions[appid]); - } - } - chrome.browserAction.getBadgeText({}, function (currentText) { - let disp = (updateCount || '') + (parseInt(currentText) || '') + ''; - chrome.browserAction.setBadgeText({ - text: disp - }, () => { - chrome.storage.local.set({ - "badge_display": disp - }, () => { - resolve(); - }); - }); - }); - }) - .catch(e => { - if (failure_callback) { - if (ext_id) - failure_callback(false, installed_versions[ext_id]); - else - failure_callback(false, {'name': ext_name}); - } - reject() - }) - }) - } + for (const [re, updaterOptions] of store_extensions) { + if (!updaterOptions.ignore) + updateUrls.push({ + 'url': updaterOptions.updateUrl, + 'name': updaterOptions.name + }); + } + function update_extension(ext_url, ext_id, ext_name) { + let is_webstore = Array.from(store_extensions.keys()).some(x => x.test(ext_url)) + return new Promise((resolve, reject) => { + fetch(ext_url) + .then(r => { + if (r.status != 200) { + return Promise.reject() + } + else + return r.text() + }) + .then(txt => new window.DOMParser().parseFromString(txt, "text/xml")) + .then(data => { + let updates = data.getElementsByTagName('app'); + let updateCount = 0; + for (let i = 0; i < updates.length; i++) { + if (updateCheck = updates[i].querySelector("*")) { + let updatever = updateCheck.getAttribute('version'); + let appid = updates[i].getAttribute('appid'); + let updatestatus = updateCheck.getAttribute('status'); + if ((updatestatus == 'ok' || !is_webstore) && updatever && installed_versions[appid] !== undefined && version_is_newer(installed_versions[appid].version, updatever)) { + updateCount++; + if (update_callback) + update_callback(updateCheck, installed_versions, appid, updatever, is_webstore); + if (appid in stored_values["removed_extensions"]) { + delete stored_values['removed_extensions'][appid]; + chrome.storage.sync.set({ + 'removed_extensions': stored_values['removed_extensions'] + }); + } + } + if (failure_callback && updatestatus == 'noupdate' && !(appid in stored_values["removed_extensions"])) + failure_callback(true, installed_versions[appid]); + } + } + chrome.browserAction.getBadgeText({}, function (currentText) { + let disp = (updateCount || '') + (parseInt(currentText) || '') + ''; + chrome.browserAction.setBadgeText({ + text: disp + }, () => { + chrome.storage.local.set({ + "badge_display": disp + }, () => { + resolve(); + }); + }); + }); + }) + .catch(e => { + if (failure_callback) { + if (ext_id) + failure_callback(false, installed_versions[ext_id]); + else + failure_callback(false, {'name': ext_name}); + } + reject() + }) + }) + } chrome.browserAction.setBadgeText({ text: '' }, () => { - let promises = updateUrls.map(uurl => update_extension(uurl.url, uurl.id, uurl.name)) - Promise.allSettled(promises) - .then(plist => { - if (plist.some(x => x.status == 'rejected')) { - chrome.browserAction.getBadgeText({}, function (currentText) { - if (!(parseInt(currentText) > 0)) - chrome.browserAction.setBadgeText({ - text: "?" - }); - }) - } - if (completed_callback) - completed_callback() - }) - }); + let promises = updateUrls.map(uurl => update_extension(uurl.url, uurl.id, uurl.name)) + Promise.allSettled(promises) + .then(plist => { + if (plist.some(x => x.status == 'rejected')) { + chrome.browserAction.getBadgeText({}, function (currentText) { + if (!(parseInt(currentText) > 0)) + chrome.browserAction.setBadgeText({ + text: "?" + }); + }) + } + if (completed_callback) + completed_callback() + }) + }); }); }); });