Skip to content

Commit

Permalink
added context menu option to install extensions from Chrome Web Store
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverDecaf committed Aug 6, 2020
1 parent aaa5746 commit 3542d44
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
Binary file modified Chromium Web Store.crx
Binary file not shown.
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"default_popup": "popup.html"
},
"content_scripts": [{
"matches": ["https://chrome.google.com/webstore*"],
"matches": ["https://chrome.google.com/webstore/*"],
"js": ["scripts/inject.js"],
"run_at": "document_end"
}],
Expand Down
19 changes: 16 additions & 3 deletions src/scripts/background.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
var extensionsDownloads = {};
function updateAll(info) {

function handleContextClick(info, tab) {
if (info.menuItemId == 'updateAll')
checkForUpdates(function (updateCheck, installed_versions, appid, updatever, is_webstore) {
let crx_url = updateCheck.getAttribute('codebase');
promptInstall(crx_url, is_webstore, extensionsDownloads);
});
else if (info.menuItemId == 'installExt')
chrome.tabs.sendMessage(tab.id, {
action: "install"
});
};

function updateBadge(modified_ext_id = null) {
checkForUpdates();
};
Expand Down Expand Up @@ -34,6 +40,11 @@ chrome.runtime.onInstalled.addListener(function () {
id: 'updateAll',
contexts: ["browser_action"]
});
chrome.contextMenus.create({
title: chrome.i18n.getMessage("webstore_addButton"),
id: 'installExt',
documentUrlPatterns: ["https://chrome.google.com/webstore/detail/*"]
});
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.downloadId) {
Expand All @@ -46,8 +57,10 @@ chrome.downloads.onChanged.addListener((d) => {
chrome.downloads.search({
id: d.id
}, (di) => {
chrome.tabs.create({url: 'file:///' + di[0].filename});
chrome.tabs.create({
url: 'file:///' + di[0].filename
});
});
}
});
chrome.contextMenus.onClicked.addListener(updateAll);
chrome.contextMenus.onClicked.addListener(handleContextClick);
15 changes: 12 additions & 3 deletions src/scripts/inject.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
function getExtensionId(url) {
return /.*detail\/[^\/]*\/([a-z]*)/i.exec(url)[1];
}

function buildExtensionUrl(extensionId) {
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';
}

function createButton(newParent) {
var button_div = document.createElement('div');
button_div.setAttribute('role', 'button');
Expand Down Expand Up @@ -37,8 +39,8 @@ function createButton(newParent) {
}
var modifyButtonObserver = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.addedNodes.length && !mutation.removedNodes.length && mutation.nextSibling == null && !mutation.addedNodes[0].className) {
if (window.location.pathname.indexOf('detail') != 10) {
if (mutation.addedNodes.length && !mutation.removedNodes.length && mutation.nextSibling == null && !mutation.addedNodes[0].className && mutation.addedNodes[0] instanceof Element) {
if (window.location.pathname.indexOf('detail') != 10 && mutation.addedNodes[0]) {
var extensionLinks = mutation.addedNodes[0].getElementsByTagName('a');
for (var i = 0; i < extensionLinks.length; i++) {
if (extensionLinks[i].getAttribute('type') == 'W') {
Expand Down Expand Up @@ -80,4 +82,11 @@ attachMainObserver = new MutationObserver(function (mutations) {
});
attachMainObserver.observe(document.body, {
childList: true
});
});
window.onload = () => {
chrome.runtime.onMessage.addListener(request => {
if (request.action === "install") {
window.open(buildExtensionUrl(getExtensionId(window.location.href)));
}
});
};

0 comments on commit 3542d44

Please sign in to comment.