Fix service workers issues

main
Elbert Alias 2 years ago
parent 761e4733f0
commit 111f6bc21f

@ -21,6 +21,12 @@ const xhrDebounce = []
let xhrAnalyzed = {} let xhrAnalyzed = {}
let initDone
const initPromise = new Promise((resolve) => {
initDone = resolve
})
function getRequiredTechnologies(name, categoryId) { function getRequiredTechnologies(name, categoryId) {
return name return name
? Wappalyzer.requires.find(({ name: _name }) => _name === name).technologies ? Wappalyzer.requires.find(({ name: _name }) => _name === name).technologies
@ -66,66 +72,34 @@ const Driver = {
}), }),
{} {}
), ),
tabs: {},
robots: await getOption('robots', {}), robots: await getOption('robots', {}),
ads: [], ads: [],
} }
chrome.action.setBadgeBackgroundColor({ color: '#6B39BD' }, () => {})
chrome.webRequest.onCompleted.addListener(
Driver.onWebRequestComplete,
{ urls: ['http://*/*', 'https://*/*'], types: ['main_frame'] },
['responseHeaders']
)
chrome.webRequest.onCompleted.addListener(Driver.onScriptRequestComplete, {
urls: ['http://*/*', 'https://*/*'],
types: ['script'],
})
chrome.webRequest.onCompleted.addListener(Driver.onXhrRequestComplete, {
urls: ['http://*/*', 'https://*/*'],
types: ['xmlhttprequest'],
})
chrome.tabs.onRemoved.addListener((id) => delete Driver.cache.tabs[id])
chrome.tabs.onUpdated.addListener(async (id, { status, url }) => {
if (status === 'complete') {
;({ url } = await promisify(chrome.tabs, 'get', id))
}
if (url) {
const { hostname } = new URL(url)
const cache = Driver.cache.hostnames[hostname]
Driver.cache.tabs[id] = cache ? resolve(cache.detections) : []
await Driver.setIcon(url, Driver.cache.tabs[id])
}
})
// Enable messaging between scripts
chrome.runtime.onMessage.addListener(Driver.onMessage)
const { version } = chrome.runtime.getManifest() const { version } = chrome.runtime.getManifest()
const previous = await getOption('version') const previous = await getOption('version')
const upgradeMessage = await getOption('upgradeMessage', true) const upgradeMessage = await getOption('upgradeMessage', true)
if (previous === null) { await setOption('version', version)
const current = await getOption('version')
if (!previous) {
await Driver.clearCache()
if (current) {
open( open(
'https://www.wappalyzer.com/installed/?utm_source=installed&utm_medium=extension&utm_campaign=wappalyzer' 'https://www.wappalyzer.com/installed/?utm_source=installed&utm_medium=extension&utm_campaign=wappalyzer'
) )
}
} else if (version !== previous && upgradeMessage) { } else if (version !== previous && upgradeMessage) {
// open( open(
// `https://www.wappalyzer.com/upgraded/?utm_source=upgraded&utm_medium=extension&utm_campaign=wappalyzer`, `https://www.wappalyzer.com/upgraded/?utm_source=upgraded&utm_medium=extension&utm_campaign=wappalyzer`,
// false false
// ) )
} }
await setOption('version', version) initDone()
}, },
/** /**
@ -327,7 +301,11 @@ const Driver = {
return return
} }
Promise.resolve(Driver[func].call(Driver[func], ...(args || []))) new Promise(async (resolve) => {
await initPromise
resolve(Driver[func].call(Driver[func], ...(args || [])))
})
.then(callback) .then(callback)
.catch(Driver.error) .catch(Driver.error)
@ -673,6 +651,7 @@ const Driver = {
await Driver.setIcon(url, resolved) await Driver.setIcon(url, resolved)
/*
if (url) { if (url) {
let tabs = [] let tabs = []
@ -686,6 +665,7 @@ const Driver = {
tabs.forEach(({ id }) => (Driver.cache.tabs[id] = resolved)) tabs.forEach(({ id }) => (Driver.cache.tabs[id] = resolved))
} }
*/
Driver.log({ hostname, technologies: resolved }) Driver.log({ hostname, technologies: resolved })
@ -788,7 +768,7 @@ const Driver = {
return return
} }
const { id, url } = tab const { url } = tab
if (await Driver.isDisabledDomain(url)) { if (await Driver.isDisabledDomain(url)) {
await Driver.setIcon(url, []) await Driver.setIcon(url, [])
@ -798,7 +778,11 @@ const Driver = {
const showCached = await getOption('showCached', true) const showCached = await getOption('showCached', true)
const resolved = (Driver.cache.tabs[id] || []).filter( const { hostname } = new URL(url)
const cache = Driver.cache.hostnames[hostname]
const resolved = (cache ? resolve(cache.detections) : []).filter(
({ cached }) => showCached || cached === false ({ cached }) => showCached || cached === false
) )
@ -905,7 +889,6 @@ const Driver = {
*/ */
async clearCache() { async clearCache() {
Driver.cache.hostnames = {} Driver.cache.hostnames = {}
Driver.cache.tabs = {}
xhrAnalyzed = {} xhrAnalyzed = {}
@ -978,4 +961,25 @@ const Driver = {
}, },
} }
chrome.action.setBadgeBackgroundColor({ color: '#6B39BD' }, () => {})
chrome.webRequest.onCompleted.addListener(
Driver.onWebRequestComplete,
{ urls: ['http://*/*', 'https://*/*'], types: ['main_frame'] },
['responseHeaders']
)
chrome.webRequest.onCompleted.addListener(Driver.onScriptRequestComplete, {
urls: ['http://*/*', 'https://*/*'],
types: ['script'],
})
chrome.webRequest.onCompleted.addListener(Driver.onXhrRequestComplete, {
urls: ['http://*/*', 'https://*/*'],
types: ['xmlhttprequest'],
})
// Enable messaging between scripts
chrome.runtime.onMessage.addListener(Driver.onMessage)
Driver.init() Driver.init()

@ -286,6 +286,8 @@ const Popup = {
return templates return templates
}, {}) }, {})
Popup.onGetDetections()
// Disabled domains // Disabled domains
const dynamicIcon = await getOption('dynamicIcon', false) const dynamicIcon = await getOption('dynamicIcon', false)
@ -315,6 +317,7 @@ const Popup = {
Popup.driver('getDetections').then(Popup.onGetDetections.bind(this)) Popup.driver('getDetections').then(Popup.onGetDetections.bind(this))
} else { } else {
el.terms.classList.remove('terms--hidden') el.terms.classList.remove('terms--hidden')
el.empty.classList.add('empty--hidden')
el.detections.classList.add('detections--hidden') el.detections.classList.add('detections--hidden')
el.issue.classList.add('issue--hidden') el.issue.classList.add('issue--hidden')
el.footer.classList.add('footer--hidden') el.footer.classList.add('footer--hidden')
@ -538,6 +541,7 @@ const Popup = {
* @param {Array} detections * @param {Array} detections
*/ */
async onGetDetections(detections = []) { async onGetDetections(detections = []) {
console.log(detections, 'xxx')
Popup.cache.detections = detections Popup.cache.detections = detections
const el = { const el = {

@ -97,7 +97,7 @@ const Utils = {
}, },
(response) => { (response) => {
chrome.runtime.lastError chrome.runtime.lastError
? reject(new Error(chrome.runtime.lastError.message)) ? reject(chrome.runtime.lastError)
: resolve(response) : resolve(response)
} }
) )