Skip to content

Commit

Permalink
firefox support & packing
Browse files Browse the repository at this point in the history
  • Loading branch information
dimdenGD committed May 11, 2023
1 parent e12f12a commit ff1e108
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 5 deletions.
1 change: 0 additions & 1 deletion content_script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

chrome.storage.sync.get(
{ instance: 'co.wukko.me', auto: true },
(items) => {
Expand Down
7 changes: 5 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ["<all_urls>"],
"js": ["content_script.js"],
"run_at": "document_idle"
"run_at": "document_end"
}
]
}
3 changes: 1 addition & 2 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
<body>
<h1>cobalt extension options</h1>
instance: <input value="co.wukko.me" id="instance" placeholder="instance"><br><br>
press on save button automatically: <input type="checkbox" id="auto" style="vertical-align:sub"><br>
<br>
press on save button automatically: <input type="checkbox" id="auto" style="vertical-align:sub"><br><br>
<button id="save">save</button>
<br><br>
<span class="smol">extension made by <a href="https://dimden.dev/" target="_blank">dimden</a></span>
Expand Down
94 changes: 94 additions & 0 deletions pack.js
Original file line number Diff line number Diff line change
@@ -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: <input type="checkbox" id="auto" style="vertical-align:sub"><br><br>', "");

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!");
});

0 comments on commit ff1e108

Please sign in to comment.