From e428b1e58cb4c45326ecff65d4c0ed4f99fe8dd4 Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 30 Apr 2021 14:24:04 +0200 Subject: [PATCH] Switch to passive messaging --- background/navigation-manager.js | 15 +++++++++ content/install-hook-userstylesworld.js | 42 ++++++------------------- manifest.json | 6 ---- 3 files changed, 24 insertions(+), 39 deletions(-) diff --git a/background/navigation-manager.js b/background/navigation-manager.js index 932dca48..78417f15 100644 --- a/background/navigation-manager.js +++ b/background/navigation-manager.js @@ -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 */ diff --git a/content/install-hook-userstylesworld.js b/content/install-hook-userstylesworld.js index 9f38a0bc..aaa98364 100644 --- a/content/install-hook-userstylesworld.js +++ b/content/install-hook-userstylesworld.js @@ -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); })(); diff --git a/manifest.json b/manifest.json index b8bdce67..aafb0dcc 100644 --- a/manifest.json +++ b/manifest.json @@ -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": {