From ff1e108a28130e9e8c7f5715ba1dd3d4baa13f66 Mon Sep 17 00:00:00 2001 From: dimden Date: Fri, 12 May 2023 01:23:01 +0300 Subject: [PATCH] firefox support & packing --- content_script.js | 1 - manifest.json | 7 +++- options.html | 3 +- pack.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 pack.js diff --git a/content_script.js b/content_script.js index ac11157..2717fa1 100644 --- a/content_script.js +++ b/content_script.js @@ -1,4 +1,3 @@ - chrome.storage.sync.get( { instance: 'co.wukko.me', auto: true }, (items) => { diff --git a/manifest.json b/manifest.json index b02242d..8f9d60c 100644 --- a/manifest.json +++ b/manifest.json @@ -21,12 +21,15 @@ }, "default_title": "download media from this page" }, - "options_page": "options.html", + "options_ui": { + "page": "options.html", + "open_in_tab": true + }, "content_scripts": [ { "matches": [""], "js": ["content_script.js"], - "run_at": "document_idle" + "run_at": "document_end" } ] } \ No newline at end of file diff --git a/options.html b/options.html index 9786ca8..64388e0 100644 --- a/options.html +++ b/options.html @@ -15,8 +15,7 @@

cobalt extension options

instance:

- press on save button automatically:
-
+ press on save button automatically:



extension made by dimden diff --git a/pack.js b/pack.js new file mode 100644 index 0000000..2655208 --- /dev/null +++ b/pack.js @@ -0,0 +1,94 @@ +/* +This script generates Firefox version of the extension and packs Chrome and Firefox versions to zip files. +Node.js v16.6.1 recommended. +*/ + +const fsp = require('fs').promises; +const fs = require('fs'); +const path = require('path'); +const AdmZip = require('adm-zip'); + +async function copyDir(src, dest) { + const entries = await fsp.readdir(src, { withFileTypes: true }); + await fsp.mkdir(dest); + for (let entry of entries) { + if(entry.name === '.git' || entry.name === '.github' || entry.name === '_metadata') continue; + const srcPath = path.join(src, entry.name); + const destPath = path.join(dest, entry.name); + if (entry.isDirectory()) { + await copyDir(srcPath, destPath); + } else { + await fsp.copyFile(srcPath, destPath); + } + } +} + +if(fs.existsSync('../CobaltExtTempChrome')) { + fs.rmSync('../CobaltExtTempChrome', { recursive: true }); +} +if(fs.existsSync('../CobaltExtFirefox')) { + fs.rmSync('../CobaltExtFirefox', { recursive: true }); +} + +console.log("Copying..."); +copyDir('./', '../CobaltExtFirefox').then(async () => { + await copyDir('./', '../CobaltExtTempChrome'); + console.log("Copied!"); + console.log("Patching..."); + + let manifest = JSON.parse(await fsp.readFile('../CobaltExtTempChrome/manifest.json', 'utf8')); + manifest.background.scripts = [manifest.background.service_worker]; + delete manifest.background.service_worker; + delete manifest.content_scripts; + + let scripts = ["content_script.js", "options.js", "background.js"]; + for(let script of scripts) { + let data = await fsp.readFile(`../CobaltExtFirefox/${script}`, 'utf8'); + data = data.replace(/chrome\.storage\.sync\./g, "chrome.storage.local."); + + if(script === "options.js") { + data = data.replace("const auto = document.getElementById('auto').checked;", "").replace(", auto: true", "").replace(", auto", "").replace("document.getElementById('auto').checked = items.auto;", ""); + } + + await fsp.writeFile(`../CobaltExtFirefox/${script}`, data); + } + + let options_html = await fsp.readFile('../CobaltExtFirefox/options.html', 'utf8'); + options_html = options_html.replace('press on save button automatically:

', ""); + + fs.writeFileSync('../CobaltExtFirefox/manifest.json', JSON.stringify(manifest, null, 2)); + fs.writeFileSync('../CobaltExtFirefox/options.html', options_html); + fs.unlinkSync('../CobaltExtFirefox/pack.js'); + fs.unlinkSync('../CobaltExtFirefox/content_script.js'); + fs.unlinkSync('../CobaltExtTempChrome/pack.js'); + + console.log("Patched!"); + if (fs.existsSync('../CobaltExtFirefox.zip')) { + console.log("Deleting old zip..."); + fs.unlinkSync('../CobaltExtFirefox.zip'); + console.log("Deleted old zip!"); + } + console.log("Zipping Firefox version..."); + try { + const zip = new AdmZip(); + const outputDir = "../CobaltExtFirefox.zip"; + zip.addLocalFolder("../CobaltExtFirefox"); + zip.writeZip(outputDir); + } catch (e) { + console.log(`Something went wrong ${e}`); + } + console.log("Zipping Chrome version..."); + try { + const zip = new AdmZip(); + const outputDir = "../CobaltExtChrome.zip"; + zip.addLocalFolder("../CobaltExtTempChrome"); + zip.writeZip(outputDir); + } catch (e) { + console.log(`Something went wrong ${e}`); + } + console.log("Zipped!"); + console.log("Deleting temporary folders..."); + fs.rmSync('../CobaltExtTempChrome', { recursive: true }); + fs.rmSync('../CobaltExtFirefox', { recursive: true }); + console.log("Deleted!"); +}); \ No newline at end of file