-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
41 lines (34 loc) · 1.49 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {patchExt} from "/patchExt.js"
chrome.action.onClicked.addListener(() => {
chrome.tabs.create({url: "dashboard.html"})
})
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
request.type == "getCWS" && getCWS(sender.tab.url).then(xpi => sendResponse(xpi))
request.type == "isInstalledCWS" && isInstalledCWS(sender.tab.url).then(bool => sendResponse(bool))
request.type == "uninstall" && uninstallCWS(sender.tab.url)
return true
})
chrome.management.onInstalled.addListener(ext => {
if(ext.id.endsWith("XPIPorter")){
chrome.tabs.reload()
}
})
chrome.management.onUninstalled.addListener(ext => {
if(ext.id.endsWith("XPIPorter")){
chrome.tabs.reload()
}
})
async function getCWS(url){
let id = url.replace(/.*?\/detail(\/.*?)?\/(.*?)(\/|#|\?|$).*/, "$2")
let crx = await fetch(`https://clients2.google.com/service/update2/crx?response=redirect&prodversion=49.0&acceptformat=crx3&x=id%3D${id}%26installsource%3Dondemand%26uc`).then(r => r.arrayBuffer())
return await patchExt(crx, id, "CWS")
}
async function isInstalledCWS(url){
let id = url.replace(/.*?\/detail(\/.*?)?\/(.*?)(\/|#|\?|$).*/, "$2")
let allExtensions = await chrome.management.getAll()
return Boolean(allExtensions.find(ext => ext.id == `${id}@CWS_XPIPorter`))
}
function uninstallCWS(url){
let id = url.replace(/.*?\/detail(\/.*?)?\/(.*?)(\/|#|\?|$).*/, "$2")
chrome.runtime.sendMessage(`${id}@CWS_XPIPorter`, {type:"XPIPorterUninstall"})
}