Skip to content

Commit

Permalink
Migrate Chrome extension to Manifest Version 3
Browse files Browse the repository at this point in the history
This concludes the migration work to Manifest version 3. Most of the
necessary changes have been made in other commits since 2022.

Because Manifest Version 3 support requires Chrome 88+, the
minimum_chrome_version has been set to 88 for clarity,
and minimum_opera_version to 74 (which matches Chrome's 88).

The Firefox version is still on Manifest Version 2 since Firefox has not
announced deprecation of Manifest Version 2.

Note: `optional_host_permissions` support was introduced in Chrome 102
(crbug.com/1265064). In Chrome 88 - 101, the extension can only view
Chrome extension sources (with host_permissions) and Firefox add-ons
(with CORS from AMO). Not extensions from other sources.

As a bonugs, another long-standing bug has been fixed: previously, if
Chrome was started with --incognito, then the extension may fail to
initialized because the event page would not wake up. Service workers
always wake up if it has not been activated before for the specific
incognito profile. Because of this, the extension will now initialize
more reliably in incognito windows.
  • Loading branch information
Rob--W committed Dec 10, 2023
1 parent 620fc24 commit 477858d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 29 deletions.
2 changes: 2 additions & 0 deletions make.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ target.firefox = function() {

cd(FIREFOX_BUILD_DIR);
removeTestFiles();
// Firefox version uses event page instead of service worker.
rm('service_worker.js');
rm('-f', '../crxviewer_firefox.zip');
exec('7z a ../crxviewer_firefox.zip * -tzip');
lintDir(FIREFOX_BUILD_DIR);
Expand Down
12 changes: 10 additions & 2 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,19 +299,27 @@ function registerEventRules() {
pathContains: "addon/"
}];

if (!chrome.declarativeContent.ShowAction) {
// Chrome < 97.
chrome.declarativeContent.ShowAction = chrome.declarativeContent.ShowPageAction;
}

var rule = {
conditions: pageUrlFilters.map(function(pageUrlFilter) {
return new chrome.declarativeContent.PageStateMatcher({
pageUrl: pageUrlFilter,
});
}),
actions: [
new chrome.declarativeContent.ShowPageAction(),
new chrome.declarativeContent.ShowAction(),
],
};

chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([rule]);
chrome.declarativeContent.onPageChanged.addRules([rule], function() {
// Visibility of action fully controlled by declarativeContent.
chrome.action.disable();
});
});
}
//// The documentation recommends to use runtime.onInstalled to register
Expand Down
2 changes: 1 addition & 1 deletion src/crxviewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2142,7 +2142,7 @@ function loadUrlInViewer(crx_url, onHasBlob) {
//#if CHROME
function maybeShowPermissionRequest() {
var permission = {
origins: ['<all_urls>']
origins: ['*://*/*']
};
chrome.permissions.contains(permission, function(hasAccess) {
if (hasAccess) return;
Expand Down
25 changes: 10 additions & 15 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
"short_name": "CRX Viewer",
"description": "View source code of Chrome extensions, Firefox addons or Opera extensions (crx/nex/xpi) from the Chrome web store and elsewhere.",
"version": "1.6.13",
"manifest_version": 2,
"manifest_version": 3,
"minimum_chrome_version": "88",
"background": {
"scripts": [
"storage-sync-polyfill.js",
"chrome-platform-info.js",
"cws_pattern.js",
"background.js",
"bg-contextmenu.js"
],
"persistent": false
"service_worker": "service_worker.js"
},
"page_action": {
"action": {
"default_icon": {
"19": "icons/19.png",
"38": "icons/38.png"
Expand All @@ -29,20 +23,21 @@
},
"options_page": "options.html",
"options_ui": {
"page": "options.html#optionsV2",
"chrome_style": true
"page": "options.html#optionsV2"
},
"permissions": [
"tabs",
"storage",
"contextMenus",
"downloads",
"declarativeContent",
"declarativeContent"
],
"host_permissions": [
"*://clients2.google.com/service/update2/crx*",
"*://clients2.googleusercontent.com/crx/download/*"
],
"optional_permissions": [
"<all_urls>"
"optional_host_permissions": [
"*://*/*"
],
"incognito": "split"
}
18 changes: 7 additions & 11 deletions src/manifest_opera.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@
"name": "Extension source viewer",
"description": "View source code of extensions in the Chrome Web Store, Firefox or Opera addon gallery. Has button to install from Chrome Web Store.",
"version": "1.6.13",
"manifest_version": 2,
"manifest_version": 3,
"minimum_opera_version": "74",
"developer": {
"name": "Rob Wu",
"url": "https://robwu.nl"
},
"background": {
"scripts": [
"storage-sync-polyfill.js",
"chrome-platform-info.js",
"cws_pattern.js",
"background.js",
"bg-contextmenu.js"
],
"persistent": false
"service_worker": "service_worker.js"
},
"page_action": {
"action": {
"default_icon": {
"19": "icons/19.png",
"38": "icons/38.png"
Expand All @@ -36,7 +30,9 @@
"storage",
"contextMenus",
"downloads",
"declarativeContent",
"declarativeContent"
],
"host_permissions": [
"*://clients2.google.com/service/update2/crx*",
"*://clients2.googleusercontent.com/crx/download/*",
"*://addons.opera.com/*",
Expand Down
4 changes: 4 additions & 0 deletions src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ function initialize() {
crx_url = get_crx_url(cws_url);
filename = get_zip_name(crx_url);
if (!can_viewsource_crx_url(crx_url)) {
//#if FIREFOX
chrome.pageAction.hide(tabs[0].id);
//#else
chrome.action.disable(tabs[0].id);
//#endif
window.close();
return;
}
Expand Down
10 changes: 10 additions & 0 deletions src/service_worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";
/* globals importScripts */

importScripts(
"storage-sync-polyfill.js",
"chrome-platform-info.js",
"cws_pattern.js",
"background.js",
"bg-contextmenu.js"
);

0 comments on commit 477858d

Please sign in to comment.