|
|
@ -2,11 +2,6 @@ import { GM_fetch, GM_head, headerStringToObject } from './requests'; |
|
|
|
|
|
|
|
export const lqueue = {} as any; |
|
|
|
|
|
|
|
const localLoad = <T>(key: string, def: T) => |
|
|
|
('__pee__' + key) in localStorage |
|
|
|
? JSON.parse(localStorage.getItem('__pee__' + key)!) as T |
|
|
|
: def; |
|
|
|
|
|
|
|
const localSet = (key: string, value: any) => |
|
|
|
localStorage.setItem('__pee__' + key, JSON.stringify(value)); |
|
|
|
|
|
|
@ -46,11 +41,20 @@ if (execution_mode != 'userscript' && !isBackground && execution_mode != 'worker |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
let msgBuff: [any, Transferable[] | undefined][] = []; |
|
|
|
|
|
|
|
export const setupPort = (port: MessagePort) => { |
|
|
|
port1 = port; |
|
|
|
port1.onmessage = (ev) => { |
|
|
|
lqueue[ev.data.id](ev.data); |
|
|
|
}; |
|
|
|
|
|
|
|
if (execution_mode == "worker") { |
|
|
|
for (const msg of msgBuff) { |
|
|
|
port.postMessage(msg[0], { transfer: msg[1] }); |
|
|
|
} |
|
|
|
msgBuff = []; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
// will be later overwritten if it's not launched from the userscript
|
|
|
@ -60,11 +64,7 @@ if (execution_mode == "worker") { |
|
|
|
lqueue[ev.data.id](ev.data); |
|
|
|
}, |
|
|
|
postMessage(msg, tr?: Transferable[]) { |
|
|
|
(postMessage as any)({ |
|
|
|
type: 'ipc', |
|
|
|
tr, |
|
|
|
msg |
|
|
|
}, tr); |
|
|
|
msgBuff.push([msg, tr]); |
|
|
|
} |
|
|
|
} as MessagePort; |
|
|
|
} |
|
|
@ -97,7 +97,9 @@ export const sendCmd = <V>(cmd: any, tr?: Transferable[], overwrite = false, tod |
|
|
|
}; |
|
|
|
|
|
|
|
const bridge = <U extends any[], V, T extends (...args: U) => V>(name: string, f: T) => { |
|
|
|
if (execution_mode != 'userscript' && !isBackground && execution_mode != 'worker') |
|
|
|
if (execution_mode == 'userscript') |
|
|
|
return f; |
|
|
|
if (isBackground) |
|
|
|
return f; |
|
|
|
// It has to be the background script
|
|
|
|
return (...args: U) => { |
|
|
@ -162,8 +164,31 @@ export class Platform { |
|
|
|
return obj.tabs.create({ active: opts.active, url: src, index: i }); |
|
|
|
} |
|
|
|
|
|
|
|
static getValue<T>(name: string, def: T) { |
|
|
|
return localLoad(name, def); |
|
|
|
static async getValue<T>(key: string, def: T) { |
|
|
|
const isinls = ('__pee__' + key) in localStorage; |
|
|
|
let ret: T; |
|
|
|
if (isinls) { |
|
|
|
let it = localStorage.getItem('__pee__' + key); |
|
|
|
if (it === "undefined") |
|
|
|
it = null; |
|
|
|
ret = { ...def, ...JSON.parse(it || '{}') } as T; |
|
|
|
} else |
|
|
|
ret = def; |
|
|
|
|
|
|
|
if (execution_mode != "userscript") { |
|
|
|
if (isinls) { |
|
|
|
delete localStorage[('__pee__' + key)]; |
|
|
|
await chrome.storage.local.set({ |
|
|
|
[key]: JSON.stringify(ret) |
|
|
|
}); |
|
|
|
} else { |
|
|
|
const d = await chrome.storage.local.get([key]); |
|
|
|
if (typeof d[key] == "string") |
|
|
|
return { ...def, ...(await JSON.parse('' + d[key] || '{}')) } as T; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
static setValue(name: string, val: any) { |
|
|
|