Switch to passive messaging
This commit is contained in:
parent
c24872a57c
commit
e428b1e58c
|
@ -66,6 +66,21 @@ bgReady.all.then(() => {
|
|||
{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
|
||||
*/
|
||||
|
|
|
@ -1,40 +1,16 @@
|
|||
'use strict';
|
||||
|
||||
(() => {
|
||||
function watchForStylusButton() {
|
||||
// Use 1 function so we won't have duplicate code around.
|
||||
const stylusQuery = () => document.querySelector('a#stylus');
|
||||
const allowedOrigin = 'https://userstyles.world';
|
||||
|
||||
if (!stylusQuery()) {
|
||||
const stylusButtonObserver = new MutationObserver(() => {
|
||||
if (stylusQuery()) {
|
||||
stylusButtonObserver.disconnect();
|
||||
stylusQuery().remove();
|
||||
}
|
||||
});
|
||||
stylusButtonObserver.observe(document.body, {childList: true, subtree: true});
|
||||
} else {
|
||||
stylusQuery().remove();
|
||||
const onPageLoaded = event => {
|
||||
if (event.data
|
||||
&& event.data.type === 'usw-remove-stylus-button'
|
||||
&& allowedOrigin === event.origin
|
||||
) {
|
||||
document.querySelector('a#stylus').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);
|
||||
})();
|
||||
|
|
|
@ -89,12 +89,6 @@
|
|||
"run_at": "document_start",
|
||||
"all_frames": false,
|
||||
"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": {
|
||||
|
|
Loading…
Reference in New Issue
Block a user