Skip to content

Commit

Permalink
rewrite to remove duplicate code, also fixes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverDecaf committed Apr 23, 2020
1 parent fb2f240 commit 4b5e4b2
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 325 deletions.
Binary file modified Chromium Web Store.crx
Binary file not shown.
4 changes: 2 additions & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "NeverDecaf",
"manifest_version": 2,
"name": "Chromium Web Store",
"version": "0.8.1",
"version": "0.9.0",
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqF/d41Q7agjkUzYq8ZGbQr8XW8mmEIMXOnR1uCTnYLL+Dm9Z+LO50xZukOISNy6zFxpI8ts/OGLsm+I2x9+UprUU4/EVdmxuwegFE6NBoEhHoRNYY0gbXZkaU8YY/XwzjVY/k18DDhl5NYPEnF6uq4Oyidg+xtd3W4+iGYczuOLER1Tp5y614zOTphcvFYhvUkCijQ6HT1TtRq/34SlFoRQqo4SFiLriK451xWIcfwiMLIekWrdoQa1v8dqIlMA3r6CKc0QykJpSYbiyormWiZ0hl2HLpkZ85mD9V0eDQ5RCtb6vkybK7INcq4yKQV4YkXhr9NpX9U4re4dlFQjEJQIDAQAB",
"description": "Allow installing/updating extensions from chrome web store on chromium based browsers. Click badge to check for updates.",
"permissions": [
Expand All @@ -26,7 +26,7 @@
"run_at": "document_start"
}],
"background": {
"scripts": ["scripts/background.js"],
"scripts": ["scripts/util.js", "scripts/background.js"],
"persistent": false
},
"web_accessible_resources": [
Expand Down
1 change: 1 addition & 0 deletions src/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</head>
<body>
<div id="app"></div>
<script src="scripts/util.js"></script>
<script src="scripts/popup.js"></script>
</body>
</html>
192 changes: 10 additions & 182 deletions src/scripts/background.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,18 @@
function updateAll(info) {
if (info.menuItemId == 'updateAll')
checkForUpdates(function (updateCheck, installed_versions, appid, updatever, is_webstore) {
let crx_url = updateCheck.getAttribute('codebase');
window.open(crx_url, '_blank');
});
};
function updateBadge(modified_ext_id = null) {
checkForUpdates();
};
chrome.browserAction.setBadgeBackgroundColor({
color: '#F00'
});

function updateBadge(modified_ext_id = null) {
chrome.management.getAll(function (e) {
var default_options = {
"auto_update": true,
"check_store_apps": true,
"check_external_apps": true
};
var chromeVersion = /Chrome\/([0-9.]+)/.exec(navigator.userAgent)[1];
var webstoreUrl = 'clients2.google.com/service/update2/crx';
var updateUrl = 'https://clients2.google.com/service/update2/crx?response=updatecheck&acceptformat=crx2,crx3&prodversion=' + chromeVersion;
var installed_versions = {};
var updateUrls = [];
e.forEach(function (ex) {
if (ex.updateUrl) {
if (webstoreUrl == ex.updateUrl.replace(/^(?:https?:\/\/)?/i, "")) {
updateUrl += '&x=id%3D' + ex.id + '%26uc';
} else {
updateUrls.push(ex.updateUrl);
}
installed_versions[ex.id] = ex;
}
default_options[ex.id] = false;
});
updateUrls.push(updateUrl);

chrome.storage.sync.get(default_options, function (settings) {
if (settings.auto_update) {
function getNewXhr() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status == 200) {
xmlDoc = this.responseXML;
var updates = xmlDoc.getElementsByTagName('app');
let updateCount = 0;
for (var i = 0; i < updates.length; i++) {
if (updateCheck = updates[i].querySelector("*")) {
var updatever = updateCheck.getAttribute('version');
var appid = updates[i].getAttribute('appid');
var is_webstore = xhttp._url == updateUrl;
if (updatever && !settings[appid] && installed_versions[appid].version != updatever) {
updateCount++;
}
}
}
chrome.browserAction.getBadgeText({}, function (currentText) {
if (currentText != '?') {
if (!currentText) {
if (updateCount)
chrome.browserAction.setBadgeText({
text: '' + updateCount
});
} else
chrome.browserAction.setBadgeText({
text: parseInt(updateCount) + parseInt(currentText) + ''
});
}
});
} else {
if (is_webstore)
chrome.browserAction.setBadgeText({
text: "?"
});
}
}
};
xhttp.overrideMimeType('application/xml');
return xhttp;
};
chrome.browserAction.setBadgeText({
text: ''
});
updateUrls.forEach(function (uurl) {
if ((updateUrl == uurl && settings.check_store_apps) || (updateUrl != uurl && settings.check_external_apps)) {
xhr = getNewXhr();
xhr.open("GET", uurl, true);
xhr._url = uurl;
xhr.send();
}
});
}
});
});
};
chrome.management.onInstalled.addListener(function (ext) {
updateBadge(ext.id);
console.log(ext);
});
chrome.management.onEnabled.addListener(function (ext) {
console.log("enabled");
console.log(ext);
});
chrome.management.onDisabled.addListener(function (ext) {
console.log("disabled");
console.log(ext);
});
chrome.management.onUninstalled.addListener(function (ext) {
updateBadge(ext.id);
Expand All @@ -111,94 +27,6 @@ chrome.alarms.onAlarm.addListener(function (alarm) {
if (alarm.name == 'cws_check_extension_updates')
updateBadge();
});

function updateAll(info) {
if (info.menuItemId == 'updateAll') {
chrome.management.getAll(function (e) {
var default_options = {
"auto_update": true,
"check_store_apps": true,
"check_external_apps": true
};
var chromeVersion = /Chrome\/([0-9.]+)/.exec(navigator.userAgent)[1];
var webstoreUrl = 'clients2.google.com/service/update2/crx';
var updateUrl = 'https://clients2.google.com/service/update2/crx?response=updatecheck&acceptformat=crx2,crx3&prodversion=' + chromeVersion;
var installed_versions = {};
var updateUrls = [];
e.forEach(function (ex) {
if (ex.updateUrl) {
if (webstoreUrl == ex.updateUrl.replace(/^(?:https?:\/\/)?/i, "")) {
updateUrl += '&x=id%3D' + ex.id + '%26uc';
} else {
updateUrls.push(ex.updateUrl);
}
installed_versions[ex.id] = ex;
}
default_options[ex.id] = false;
});
updateUrls.push(updateUrl);

chrome.storage.sync.get(default_options, function (settings) {
function getNewXhr() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status == 200) {
xmlDoc = this.responseXML;
var updates = xmlDoc.getElementsByTagName('app');
let updateCount = 0;
for (var i = 0; i < updates.length; i++) {
if (updateCheck = updates[i].querySelector("*")) {
var updatever = updateCheck.getAttribute('version');
var appid = updates[i].getAttribute('appid');
var is_webstore = xhttp._url == updateUrl;
if (updatever && !settings[appid] && installed_versions[appid].version != updatever) {
updateCount++;
let crx_url = updateCheck.getAttribute('codebase');
window.open(crx_url, '_blank');
}
}
}
chrome.browserAction.getBadgeText({}, function (currentText) {
if (currentText != '?') {
if (!currentText) {
if (updateCount)
chrome.browserAction.setBadgeText({
text: '' + updateCount
});
} else
chrome.browserAction.setBadgeText({
text: parseInt(updateCount) + parseInt(currentText) + ''
});
}
});
} else {
container.appendChild(updatefailure);
if (is_webstore)
chrome.browserAction.setBadgeText({
text: "?"
});
}
}
};
xhttp.overrideMimeType('application/xml');
return xhttp;
};
chrome.browserAction.setBadgeText({
text: ''
});
updateUrls.forEach(function (uurl) {
if ((updateUrl == uurl && settings.check_store_apps) || (updateUrl != uurl && settings.check_external_apps)) {
xhr = getNewXhr();
xhr.open("GET", uurl, true);
xhr._url = uurl;
xhr.send();
}
});
});
});
}
};
chrome.runtime.onInstalled.addListener(function () {
chrome.contextMenus.create({
title: "Update All Extensions",
Expand Down
Loading

0 comments on commit 4b5e4b2

Please sign in to comment.