Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
igorlogius committed May 17, 2024
1 parent cc7f816 commit e60e9aa
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 225 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ jobs:
ISSUER: ${{secrets.ISSUER}}
SECRET: ${{secrets.SECRET}}
run: curl 'https://raw.githubusercontent.com/igorlogius/meta-addon-builder/main/README' | sh

58 changes: 29 additions & 29 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,20 @@ function isOnRegexList(url) {
}

async function buildRegExList() {
let selectors = await getFromStorage("object", "selectors", []);

const out = [];

selectors.forEach((e) => {
// check activ
if (typeof e.activ !== "boolean") {
return;
}
if (e.activ !== true) {
return;
}

// check url regex
if (typeof e.url_regex !== "string") {
return;
}
e.url_regex = e.url_regex.trim();
if (e.url_regex === "") {
return;
}

try {
out.push(new RegExp(e.url_regex));
} catch (e) {
return;
}
});

(await getFromStorage("string", "textarea_regexstrs", ""))
.split("\n")
.forEach((line) => {
line = line.trim();
if (line !== "") {
try {
out.push(new RegExp(line));
} catch (e) {
// todo: show a notification that a regex failed to compile ...
console.warn(e);
}
}
});
return out;
}

Expand Down Expand Up @@ -251,10 +236,25 @@ async function onStorageChange() {
}

// show the user the options page on first installation
function onInstall(details) {
async function onInstall(details) {
if (details.reason === "install") {
browser.runtime.openOptionsPage();
}
// convert storage data
if (details.reason === "update") {
let selectors = await getFromStorage("object", "selectors", []);

out = "";
selectors.forEach((e) => {
if (typeof e.url_regex === "string") {
out = out + e.url_regex + "\n";
}
});

if (out !== "") {
setToStorage("textarea_regexstrs", out);
}
}
}

async function onCommand(command) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
"tabs"
],
"optional_permissions": ["bookmarks", "history"],
"version": "1.1.31"
"version": "1.1.32"
}
51 changes: 17 additions & 34 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
body {
background-color: lightgrey;
}
textarea {
width: 100%;
height: 15vh;
}
</style>
</head>
<body>
Expand Down Expand Up @@ -54,7 +58,7 @@ <h4>
</button>
</fieldset>

<form id="mainForm" accept-charset="utf-8" style="display: none">
<div id="mainForm" accept-charset="utf-8" style="display: none">
<fieldset>
<b>Multi-Open shortcut argument:</b>
<input type="number" id="multiopen" value="3" min="0" max="10" />
Expand All @@ -70,41 +74,20 @@ <h4>Operation Mode</h4>
Open everything not matching in a temp container
</option>
</select>
<br />
<textarea
id="textarea_regexstrs"
placeholder="Enter regular expressions line by line here
Examples:
^https:\/\/www\.facebook\.com/.*
^https:\/\/example\.net/.*
^https:\/\/.*\.wikipedia\.org/.*
"
></textarea>
<button id="regexstrs_save_btn" style="float: right">Save</button>
</fieldset>
<fieldset>
<input
id="impbtn_wrapper"
type="button"
name="impbtn"
value="Import"
style="margin-left: 1%; width: 32%; text-align: center"
/>
<input
id="impbtn"
type="file"
name="impbtn"
value=""
style="display: none"
/>
<input
type="submit"
value="Save"
style="margin-left: 0%; width: 32%; text-align: center"
/>
<input
id="expbtn"
type="button"
name="impbtn"
value="Export"
style="margin-left: 0%; width: 32%; text-align: center"
/>
</fieldset>

<table id="mainTable" border="1" style="width: 100%">
<tbody id="mainTableBody"></tbody>
</table>
</fieldset>
</form>
</div>

<script src="options.js"></script>
</body>
194 changes: 33 additions & 161 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
/* global browser */

async function getFromStorage(type, id, fallback) {
let tmp = await browser.storage.local.get(id);
return typeof tmp[id] === type ? tmp[id] : fallback;
}

async function setToStorage(id, value) {
let obj = {};
obj[id] = value;
return browser.storage.local.set(obj);
}

function onChange(evt) {
let id = evt.target.id;
let el = document.getElementById(id);
Expand Down Expand Up @@ -29,6 +40,27 @@ function onChange(evt) {
browser.storage.local.set(obj).catch(console.error);
}

document.getElementById("regexstrs_save_btn").addEventListener("click", () => {
const val = document.getElementById("textarea_regexstrs").value.trim();
setToStorage("textarea_regexstrs", val);
});

browser.storage.local
.get("textarea_regexstrs")
.then((obj) => {
let el = document.getElementById("textarea_regexstrs");
let val = obj["textarea_regexstrs"];

if (typeof val !== "undefined") {
if (el.type === "checkbox") {
el.checked = val;
} else {
el.value = val;
}
}
})
.catch(console.error);

["multiopen"].map((id) => {
browser.storage.local
.get(id)
Expand Down Expand Up @@ -82,167 +114,7 @@ function onChange(evt) {
});
});

function deleteRow(rowTr) {
mainTableBody.removeChild(rowTr);
}

function createTableRow(feed) {
var tr = mainTableBody.insertRow();
var input;

Object.keys(feed)
.sort()
.forEach((key) => {
if (key === "activ") {
input = document.createElement("input");
input.className = key;
input.placeholder = key;
input.style.width = "100%";
input.type = "checkbox";
input.checked = typeof feed[key] === "boolean" ? feed[key] : true;
tr.insertCell().appendChild(input);
} else if (key !== "action") {
input = document.createElement("input");
input.className = key;
input.placeholder = key;
input.style.width = "100%";
input.value = feed[key];
tr.insertCell().appendChild(input);
}
});

var button;
if (feed.action === "save") {
button = createButton("ADD", "saveButton", function () {}, true);
} else {
button = createButton(
"DELETE",
"deleteButton",
function () {
deleteRow(tr);
},
false
);
}
tr.insertCell().appendChild(button);
}

function collectConfig() {
var feeds = [];
for (var row = 0; row < mainTableBody.rows.length; row++) {
try {
var url_regex = mainTableBody.rows[row]
.querySelector(".url_regex")
.value.trim();
var check = mainTableBody.rows[row].querySelector(".activ").checked;
if (url_regex !== "") {
feeds.push({
activ: check,
url_regex: url_regex,
});
}
} catch (e) {
console.error(e);
}
}
return feeds;
}

function createButton(text, id, callback, submit) {
var span = document.createElement("span");
var button = document.createElement("button");
button.id = id;
button.textContent = text;
button.className = "browser-style";
if (submit) {
button.type = "submit";
} else {
button.type = "button";
}
button.name = id;
button.value = id;
button.addEventListener("click", callback);
span.appendChild(button);
return span;
}

async function saveOptions(/*e*/) {
var config = collectConfig();
await browser.storage.local.set({ selectors: config });
}

async function restoreOptions() {
createTableRow({
activ: 1,
url_regex: "",
action: "save",
});
var res = await browser.storage.local.get("selectors");
if (!Array.isArray(res.selectors)) {
res = {
selectors: [
{
activ: false,
url_regex: "^https:\\/\\/twitch\\.tv.*",
},

{
activ: false,
url_regex: "^https:\\/\\/www\\.youtube\\.com\\/watch\\/.*",
},
],
};
}
res.selectors.forEach((selector) => {
selector.action = "delete";
createTableRow(selector);
});
}

document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("form").addEventListener("submit", saveOptions);

const impbtnWrp = document.getElementById("impbtn_wrapper");
const impbtn = document.getElementById("impbtn");
const expbtn = document.getElementById("expbtn");

expbtn.addEventListener("click", async function () {
var dl = document.createElement("a");
var res = await browser.storage.local.get("selectors");
var content = JSON.stringify(res.selectors);
dl.setAttribute(
"href",
"data:application/json;charset=utf-8," + encodeURIComponent(content)
);
dl.setAttribute("download", "data.json");
dl.setAttribute("visibility", "hidden");
dl.setAttribute("display", "none");
document.body.appendChild(dl);
dl.click();
document.body.removeChild(dl);
});

// delegate to real Import Button which is a file selector
impbtnWrp.addEventListener("click", function () {
impbtn.click();
});

impbtn.addEventListener("input", function () {
var file = this.files[0];
var reader = new FileReader();
reader.onload = async function () {
try {
var config = JSON.parse(reader.result);
await browser.storage.local.set({ selectors: config });
document.querySelector("form").submit();
} catch (e) {
console.error("error loading file: " + e);
}
};
reader.readAsText(file);
});

document.getElementById("experimentalbtn").addEventListener("click", () => {
document.getElementById("experimentalbtn").addEventListener("click", (el) => {
const dp = document.getElementById("mainForm").style.display;
if (dp === "none") {
document.getElementById("mainForm").style.display = "block";
Expand Down

0 comments on commit e60e9aa

Please sign in to comment.