Skip to content

Commit

Permalink
Replace event_rules with declarativeContent #121
Browse files Browse the repository at this point in the history
Works arounds crbug.com/1132684.
  • Loading branch information
Rob--W committed Apr 18, 2022
1 parent 1051b1f commit b87b703
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 148 deletions.
64 changes: 64 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,68 @@ function isPageActionNeededForUrl(url) {
return cws_pattern.test(url) || mea_pattern.test(url) || ows_pattern.test(url) ||
amo_pattern.test(url) || atn_pattern.test(url);
}
//#else
//// Work-around for crbug.com/1132684: static event_rules disappear after a
//// restart, so we register rules dynamically instead, on install.
function registerEventRules() {
if (registerEventRules.hasRunOnce) {
return;
}
registerEventRules.hasRunOnce = true;

var pageUrlFilters = [{
hostEquals: "chrome.google.com",
pathPrefix: "/webstore/detail/"
}, {
hostEquals: "microsoftedge.microsoft.com",
pathPrefix: "/webstore/detail/"
}, {
hostEquals: "addons.opera.com",
pathContains: "extensions/details/"
}, {
hostEquals: "addons.mozilla.org",
pathContains: "addon/"
}, {
hostSuffix: "addons.mozilla.org",
pathContains: "review/"
}, {
hostEquals: "addons.allizom.org",
pathContains: "addon/"
}, {
hostSuffix: "addons.allizom.org",
pathContains: "review/"
}, {
hostEquals: "addons-dev.allizom.org",
pathContains: "addon/"
}, {
hostSuffix: "addons-dev.allizom.org",
pathContains: "review/"
}, {
hostEquals: "addons.thunderbird.net",
pathContains: "addon/"
}, {
hostSuffix: "addons-stage.thunderbird.net",
pathContains: "addon/"
}];

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

chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([rule]);
});
}
//// Work-around for crbug.com/388231 is in incognito-events.js
chrome.runtime.onInstalled.addListener(registerEventRules);
//// Work-around for crbug.com/264963: onInstalled is not fired when the
//// extension was disabled during an update.
chrome.runtime.onStartup.addListener(registerEventRules);
//#endif
3 changes: 1 addition & 2 deletions src/cws_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ var atn_match_patterns = [
// page_action.show_matches (in manifest_firefox.json) uses:
// cws_match_pattern, mea_match_pattern, ows_match_pattern, amo_match_patterns
//
// event_rules / declarativeContent (in manifest.json / manifest_opera.json)
// uses the same patterns, translated to a UrlFilter.
// declarativeContent (in background.js) uses the same patterns, translated to a UrlFilter.
//
// popup.js uses can_viewsource_crx_url to determine whether the URL can actually be opened,
// which use regexps that may be stricter than the match patterns.
Expand Down
74 changes: 1 addition & 73 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,79 +23,6 @@
"default_title": "Inspect contents of the current browser extension",
"default_popup": "popup.html"
},
"event_rules": [{
"event": "declarativeContent.onPageChanged",
"actions": [{
"type": "declarativeContent.ShowPageAction"
}],
"conditions": [{
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "chrome.google.com",
"pathPrefix": "/webstore/detail/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "microsoftedge.microsoft.com",
"pathPrefix": "/webstore/detail/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "addons.opera.com",
"pathContains": "extensions/details/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "addons.mozilla.org",
"pathContains": "addon/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostSuffix": "addons.mozilla.org",
"pathContains": "review/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "addons.allizom.org",
"pathContains": "addon/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostSuffix": "addons.allizom.org",
"pathContains": "review/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "addons-dev.allizom.org",
"pathContains": "addon/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostSuffix": "addons-dev.allizom.org",
"pathContains": "review/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "addons.thunderbird.net",
"pathContains": "addon/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostSuffix": "addons-stage.thunderbird.net",
"pathContains": "addon/"
}
}]
}],
"icons": {
"16": "icons/16.png",
"48": "icons/48.png",
Expand All @@ -111,6 +38,7 @@
"storage",
"contextMenus",
"downloads",
"declarativeContent",
"*://clients2.google.com/service/update2/crx*",
"*://clients2.googleusercontent.com/crx/download/*"
],
Expand Down
74 changes: 1 addition & 73 deletions src/manifest_opera.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,79 +26,6 @@
"default_title": "Inspect contents of the current browser extension",
"default_popup": "popup.html"
},
"event_rules": [{
"event": "declarativeContent.onPageChanged",
"actions": [{
"type": "declarativeContent.ShowPageAction"
}],
"conditions": [{
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "chrome.google.com",
"pathPrefix": "/webstore/detail/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "microsoftedge.microsoft.com",
"pathPrefix": "/webstore/detail/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "addons.opera.com",
"pathContains": "extensions/details/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "addons.mozilla.org",
"pathContains": "addon/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostSuffix": "addons.mozilla.org",
"pathContains": "review/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "addons.allizom.org",
"pathContains": "addon/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostSuffix": "addons.allizom.org",
"pathContains": "review/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "addons-dev.allizom.org",
"pathContains": "addon/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostSuffix": "addons-dev.allizom.org",
"pathContains": "review/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostEquals": "addons.thunderbird.net",
"pathContains": "addon/"
}
}, {
"type": "declarativeContent.PageStateMatcher",
"pageUrl": {
"hostSuffix": "addons-stage.thunderbird.net",
"pathContains": "addon/"
}
}]
}],
"icons": {
"16": "icons/16.png",
"48": "icons/48.png",
Expand All @@ -110,6 +37,7 @@
"storage",
"contextMenus",
"downloads",
"declarativeContent",
"*://clients2.google.com/service/update2/crx*",
"*://clients2.googleusercontent.com/crx/download/*",
"*://addons.opera.com/*",
Expand Down

0 comments on commit b87b703

Please sign in to comment.