diff --git a/.gitignore b/.gitignore index e99d1b7..2a35195 100644 --- a/.gitignore +++ b/.gitignore @@ -133,3 +133,4 @@ dist .pnp.* +dist.zip diff --git a/README.md b/README.md index f0097a8..b577535 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # SubSubDeeCee -A Firefox extension that adds multilingual subtitles to WWDC videos. +A browser extension that adds multilingual subtitles to WWDC videos. ## How to use diff --git a/background.js b/background.js index 691ba21..77aa07b 100644 --- a/background.js +++ b/background.js @@ -1,34 +1,23 @@ // @ts-check +/// -/** - * @param {import("webextension-polyfill").WebRequest.OnHeadersReceivedDetailsType} details - */ -function handleHeaders(details) { - if (!details.responseHeaders) { - return; - } - for (let i = 0; i < details.responseHeaders.length; i++) { - if (details.responseHeaders[i].name.toLowerCase() === "content-security-policy") { - // @ts-ignore - details.responseHeaders[i].value = details.responseHeaders[i].value.replace("default-src 'self'", "default-src 'self' tatzyr.github.io") - } - } - - return { responseHeaders: details.responseHeaders } -} - -function init() { - /** - * @type {import("webextension-polyfill").Browser} - */ - // @ts-ignore - const b = chrome; +const b = globalThis.browser ?? globalThis.chrome; - /** - * @type {import("webextension-polyfill").WebRequest.RequestFilter} - */ - const filter = { urls: ['https://developer.apple.com/*'], types: ['main_frame', 'sub_frame'] }; - b.webRequest.onHeadersReceived.addListener(handleHeaders, filter, ["blocking", "responseHeaders"]); -} - -init(); +b.runtime.onMessage.addListener((message, sender, sendResponse) => { + (async () => { + if (message.action === "fetchSubtitles" && message.url) { + try { + const res = await fetch(message.url); + if (!res.ok) { + throw new Error(`HTTP Error: status: ${res.status}, url: ${message.url}`); + } + // @ts-ignore + sendResponse({ data: await res.text(), error: null }); + } catch (e) { + // @ts-ignore + sendResponse({ data: null, error: { name: e.name, message: e.message } }); + } + } + })(); + return true; +}); diff --git a/content.js b/content.js index 6610397..2917d94 100644 --- a/content.js +++ b/content.js @@ -1,4 +1,5 @@ // @ts-check +/// (function () { async function main() { @@ -17,18 +18,21 @@ await new Promise((resolve) => { setTimeout(resolve, 100); }); } - video.setAttribute("crossorigin", "anonymous"); for (const lang of ["ja", "ko", "vi"]) { + const b = globalThis.browser ?? globalThis.chrome; + const { data, error } = await b.runtime.sendMessage({ + action: "fetchSubtitles", + url: `https://tatzyr.github.io/subsubdeecee-vtts/${event}/${lang}/${videoId}.vtt`, + }); + if (error) { + console.error(`Failed to fetch subtitles. ${error.name}: ${error.message}`); + continue; + } const track = document.createElement("track"); - track.setAttribute("src", `https://tatzyr.github.io/subsubdeecee-vtts/${event}/${lang}/${videoId}.vtt`); + track.setAttribute("src", URL.createObjectURL(new Blob([data], { type: "text/vtt" }))); track.setAttribute("srclang", lang); track.setAttribute("label", `SubSubDeeCee (${lang})`); - track.addEventListener("error", () => { - console.error(`Failed to load WebVTT file for ${lang}`); - video.removeChild(track); - }); - video.appendChild(track); } } diff --git a/dist.sh b/dist.sh index fea62c1..f9fa5f9 100755 --- a/dist.sh +++ b/dist.sh @@ -1,8 +1,11 @@ #!/bin/bash -mkdir dist +set -euxo pipefail + +rm -f dist.zip +mkdir -p dist rm dist/* -cp background.js content.js icon48.png icon96.png LICENSE manifest.json README.md dist +cp background.js content.js icon16.png icon48.png icon96.png icon128.png manifest.json dist pushd dist zip -r ../dist.zip * popd diff --git a/icon128.png b/icon128.png new file mode 100644 index 0000000..4887cfe Binary files /dev/null and b/icon128.png differ diff --git a/icon16.png b/icon16.png new file mode 100644 index 0000000..c710eaf Binary files /dev/null and b/icon16.png differ diff --git a/icon48.png b/icon48.png index ef079c1..8c51d38 100644 Binary files a/icon48.png and b/icon48.png differ diff --git a/icon96.png b/icon96.png index 7d68a17..64ec8db 100644 Binary files a/icon96.png and b/icon96.png differ diff --git a/manifest.json b/manifest.json index fe60bdb..51857d8 100644 --- a/manifest.json +++ b/manifest.json @@ -1,29 +1,27 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "SubSubDeeCee", "version": "1.0.2", "description": "Multilingual subtitles to WWDC videos", - "developer": { - "name": "Tatsuya Otsuka", - "url": "https://github.com/tatzyr" - }, - "permissions": [ - "webRequest", - "webRequestBlocking", - "https://developer.apple.com/*" - ], "background": { "scripts": ["background.js"], - "persistent": true + "service_worker": "background.js" + }, + "browser_specific_settings": { + "gecko": { + "id": "{14d11436-34b3-4d84-b458-53bad54871ab}" + } }, "content_scripts": [ { - "matches": ["https://developer.apple.com/*"], + "matches": ["https://developer.apple.com/videos/play/*"], "js": ["content.js"] } ], "icons": { + "16": "icon16.png", "48": "icon48.png", - "96": "icon96.png" + "96": "icon96.png", + "128": "icon128.png" } } diff --git a/package-lock.json b/package-lock.json index 17bd5d1..160da01 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,21 +5,21 @@ "packages": { "": { "devDependencies": { - "@types/webextension-polyfill": "^0.10.0" + "@types/webextension-polyfill": "^0.10.7" } }, "node_modules/@types/webextension-polyfill": { - "version": "0.10.0", - "resolved": "https://registry.npmjs.org/@types/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz", - "integrity": "sha512-If4EcaHzYTqcbNMp/FdReVdRmLL/Te42ivnJII551bYjhX19bWem5m14FERCqdJA732OloGuxCRvLBvcMGsn4A==", + "version": "0.10.7", + "resolved": "https://registry.npmjs.org/@types/webextension-polyfill/-/webextension-polyfill-0.10.7.tgz", + "integrity": "sha512-10ql7A0qzBmFB+F+qAke/nP1PIonS0TXZAOMVOxEUsm+lGSW6uwVcISFNa0I4Oyj0884TZVWGGMIWeXOVSNFHw==", "dev": true } }, "dependencies": { "@types/webextension-polyfill": { - "version": "0.10.0", - "resolved": "https://registry.npmjs.org/@types/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz", - "integrity": "sha512-If4EcaHzYTqcbNMp/FdReVdRmLL/Te42ivnJII551bYjhX19bWem5m14FERCqdJA732OloGuxCRvLBvcMGsn4A==", + "version": "0.10.7", + "resolved": "https://registry.npmjs.org/@types/webextension-polyfill/-/webextension-polyfill-0.10.7.tgz", + "integrity": "sha512-10ql7A0qzBmFB+F+qAke/nP1PIonS0TXZAOMVOxEUsm+lGSW6uwVcISFNa0I4Oyj0884TZVWGGMIWeXOVSNFHw==", "dev": true } } diff --git a/package.json b/package.json index 24c41ac..a8ed12b 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { "devDependencies": { - "@types/webextension-polyfill": "^0.10.0" + "@types/webextension-polyfill": "^0.10.7" } } diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..92b6f09 --- /dev/null +++ b/types.d.ts @@ -0,0 +1 @@ +declare var browser: import("webextension-polyfill").Browser;