-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
501 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import createPort from '/js/port'; | ||
|
||
const offscreen = /** @type {OffscreenAPI} */ new Proxy({ | ||
get exec() { | ||
const url = new URL('offscreen.html', location).href; | ||
const res = createPort(async () => { | ||
let client; | ||
for (let retry = 0; retry < 2; retry++) { | ||
client = (await self.clients.matchAll({includeUncontrolled: true})) | ||
.find(c => c.url === url); | ||
if (client || retry) { | ||
return client; | ||
} | ||
try { | ||
await chrome.offscreen.createDocument({ | ||
url, | ||
reasons: ['BLOBS', 'DOM_PARSER', 'MATCH_MEDIA', 'WORKERS'], | ||
justification: 'ManifestV3 requirement', | ||
}); | ||
} catch (err) { | ||
if (!err.message.startsWith('Only a single offscreen')) throw err; | ||
} | ||
} | ||
}); | ||
Object.defineProperty(this, 'exec', {value: res}); | ||
return res; | ||
}, | ||
}, { | ||
get: (me, cmd) => function (...args) { | ||
return me.exec.call(this, cmd, args); | ||
}, | ||
}); | ||
|
||
export default offscreen; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import './intro'; | ||
import * as msg from '/js/msg'; | ||
import './keep-alive'; | ||
import './bg-offscreen'; | ||
import '../background'; | ||
import {URLS} from '/js/toolbox'; | ||
|
||
/** @param {ExtendableEvent} evt */ | ||
self.oninstall = evt => { | ||
evt.addRoutes({ | ||
condition: {not: {urlPattern: `${URLS.ownOrigin}*.user.css`}}, | ||
source: 'network', | ||
}); | ||
}; | ||
|
||
/** @param {FetchEvent} evt */ | ||
self.onfetch = evt => { | ||
let url = evt.request.url; | ||
if (url.startsWith(URLS.ownOrigin) | ||
&& +(url = url.split('#'))[1] | ||
&& url[0].endsWith('.user.css') | ||
&& !url[0].includes('?') /* skipping installer */) { | ||
evt.respondWith(Response.redirect('edit.html?id=' + url[1])); | ||
} | ||
}; | ||
|
||
self.onmessage = evt => { | ||
if (evt.data[0] === 'port') { | ||
chrome.runtime.connect({name: evt.data[1]}); | ||
evt.ports[0].onmessage = onClientPortMessage; | ||
evt.ports[0].postMessage({id: 0}); | ||
} | ||
}; | ||
|
||
/** | ||
* @this {MessagePort} | ||
* @param {MessageEvent} evt | ||
*/ | ||
async function onClientPortMessage(evt) { | ||
const {args, id} = evt.data; | ||
let res, err; | ||
try { | ||
res = msg._execute('extension', ...args, {}); | ||
if (res instanceof Promise) res = await res; | ||
} catch (e) { | ||
err = e; | ||
res = undefined; | ||
} | ||
this.postMessage({id, res, err}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
self.browser = chrome; | ||
self.window = self; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {subscribe} from '/src/js/prefs'; | ||
|
||
/** @type {?Promise[]} */ | ||
let busy; | ||
let lastBusyTime; | ||
let pulse; | ||
|
||
subscribe('keepAlive', checkPref, true); | ||
|
||
export function keepAliveWhileBusy(...promises) { | ||
if (!busy) checkBusyWhenSettled(promises); | ||
else busy.push(...promises); | ||
} | ||
|
||
function checkBusyWhenSettled(promises) { | ||
Promise.allSettled(busy = promises).then(checkBusy); | ||
} | ||
|
||
function checkBusy({length}) { | ||
if (length < busy.length) { | ||
checkBusyWhenSettled(busy.slice(length)); | ||
} else { | ||
busy = null; | ||
lastBusyTime = performance.now(); | ||
} | ||
} | ||
|
||
async function checkPref(key, val) { | ||
if (busy || val < 0 || val && (performance.now() - lastBusyTime < val * 60e3)) { | ||
chrome.runtime.getPlatformInfo(); | ||
if (!pulse) pulse = setInterval(checkPref, 25e3, key, val); | ||
} else if (pulse) { | ||
clearInterval(pulse); | ||
pulse = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= htmlWebpackPlugin.tags.headTags %> | ||
<%= htmlWebpackPlugin.tags.bodyTags %> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.