From 5501efb1beb37d51e296c165e40ea7785dc74343 Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 3 Oct 2020 15:05:40 +0300 Subject: [PATCH] expose version for styles installed from greasyfork/sleazyfork --- background/content-scripts.js | 15 +++++++++++++++ content/install-hook-greasyfork.js | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 content/install-hook-greasyfork.js diff --git a/background/content-scripts.js b/background/content-scripts.js index d3c93b40..3aeecd16 100644 --- a/background/content-scripts.js +++ b/background/content-scripts.js @@ -17,6 +17,21 @@ const contentScripts = (() => { } const busyTabs = new Set(); let busyTabsTimer; + + // expose version on greasyfork/sleazyfork 1) info page and 2) code page + const urlMatches = '/scripts/\\d+[^/]*(/code)?([?#].*)?$'; + chrome.webNavigation.onCommitted.addListener(({tabId}) => { + chrome.tabs.executeScript(tabId, { + file: '/content/install-hook-greasyfork.js', + runAt: 'document_start', + }); + }, { + url: [ + {hostEquals: 'greasyfork.org', urlMatches}, + {hostEquals: 'sleazyfork.org', urlMatches}, + ] + }); + return {injectToTab, injectToAllTabs}; function injectToTab({url, tabId, frameId = null}) { diff --git a/content/install-hook-greasyfork.js b/content/install-hook-greasyfork.js new file mode 100644 index 00000000..9e125530 --- /dev/null +++ b/content/install-hook-greasyfork.js @@ -0,0 +1,21 @@ +/* global API */ +'use strict'; + +// onCommitted may fire twice +// Note, we're checking against a literal `1`, not just `if (truthy)`, +// because is exposed per HTML spec as a global variable and `window.INJECTED`. + +if (window.INJECTED_GREASYFORK !== 1) { + window.INJECTED_GREASYFORK = 1; + addEventListener('message', async function onMessage(e) { + if (e.origin === location.origin && + e.data && + e.data.name && + e.data.type === 'style-version-query') { + removeEventListener('message', onMessage); + const style = await API.findUsercss(e.data) || {}; + const {version} = style.usercssData || {}; + postMessage({type: 'style-version', version}, '*'); + } + }); +}