Switch to passive messaging

This commit is contained in:
Gusted 2021-04-30 14:24:04 +02:00
parent c24872a57c
commit e428b1e58c
No known key found for this signature in database
GPG Key ID: FD821B732837125F
3 changed files with 24 additions and 39 deletions

View File

@ -66,6 +66,21 @@ bgReady.all.then(() => {
{hostEquals: 'sleazyfork.org', urlMatches}, {hostEquals: 'sleazyfork.org', urlMatches},
], ],
}); });
/*
* Removes the Get Stylus button on style pages.
* Not using manifest.json as adding a content script disables the extension on update.
*/
chrome.webNavigation.onCommitted.addListener(({tabId}) => {
chrome.tabs.executeScript(tabId, {
file: '/content/install-hook-userstylesworld.js',
runAt: 'document_start',
});
}, {
url: [
{hostEquals: 'userstyles.world', urlMatches: '/style/\\d+[^/]*/[a-z-0-9]+$'},
],
});
/* /*
* FF misses some about:blank iframes so we inject our content script explicitly * FF misses some about:blank iframes so we inject our content script explicitly
*/ */

View File

@ -1,40 +1,16 @@
'use strict'; 'use strict';
(() => { (() => {
function watchForStylusButton() { const allowedOrigin = 'https://userstyles.world';
// Use 1 function so we won't have duplicate code around.
const stylusQuery = () => document.querySelector('a#stylus');
if (!stylusQuery()) { const onPageLoaded = event => {
const stylusButtonObserver = new MutationObserver(() => { if (event.data
if (stylusQuery()) { && event.data.type === 'usw-remove-stylus-button'
stylusButtonObserver.disconnect(); && allowedOrigin === event.origin
stylusQuery().remove(); ) {
} document.querySelector('a#stylus').remove();
});
stylusButtonObserver.observe(document.body, {childList: true, subtree: true});
} else {
stylusQuery().remove();
} }
} };
// Some trickery to make sure that the DOM is ready(document.body/document.head).
// And can possibly observe it for a stylus button.
function isDOMReady() {
return document.readyState === 'complete' || document.readyState === 'interactive';
}
if (!isDOMReady()) {
const onReadyStateChange = () => {
if (isDOMReady()) {
document.removeEventListener('readystatechange', onReadyStateChange);
watchForStylusButton();
}
};
document.addEventListener('readystatechange', onReadyStateChange);
} else {
watchForStylusButton();
}
window.addEventListener('message', onPageLoaded);
})(); })();

View File

@ -89,12 +89,6 @@
"run_at": "document_start", "run_at": "document_start",
"all_frames": false, "all_frames": false,
"js": ["content/install-hook-openusercss.js"] "js": ["content/install-hook-openusercss.js"]
},
{
"matches": ["https://userstyles.world/style/*"],
"run_at": "document_start",
"all_frames": false,
"js": ["content/install-hook-userstylesworld.js"]
} }
], ],
"browser_action": { "browser_action": {