Skip to content

Commit

Permalink
fix(#14): restore auto modes
Browse files Browse the repository at this point in the history
  • Loading branch information
igorlogius committed Mar 15, 2024
1 parent 0c9a6bc commit 69510bf
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 32 deletions.
68 changes: 60 additions & 8 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/* global browser */

// cookieStoreIds of all managed containers
let mode = null;
let historyPermissionEnabled = false;
let intId = null;
let historyCleanUpQueue = [];
let containerCleanupTimer = null;
let opennewtab = false;
let deldelay = 30000; // delay until Tmp Containers and History Entries are removed
let multiopen = 3;
let regexList = null;

// array of all allowed container colors
const allcolors = [
"blue",
Expand All @@ -24,6 +27,48 @@ const historyPermission = {
permissions: ["history"],
};

function isOnRegexList(url) {
for (let i = 0; i < regexList.length; i++) {
if (regexList[i].test(url)) {
return true;
}
}
return false;
}

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

return out;
}

async function getFromStorage(type, id, fallback) {
let tmp = await browser.storage.local.get(id);
return typeof tmp[id] === type ? tmp[id] : fallback;
Expand Down Expand Up @@ -169,9 +214,12 @@ async function onStorageChange() {
if (usecolors.length < 1) {
usecolors = allcolors;
}
//deldelay = await getFromStorage("number", "deldelay", 30000);
multiopen = await getFromStorage("number", "multiopen", 3);

mode = !(await getFromStorage("boolean", "mode", false));

regexList = await buildRegExList();

historyCleanUpQueue = await getFromStorage(
"object",
"historyCleanUpQueue",
Expand Down Expand Up @@ -213,12 +261,6 @@ async function onCommand(command) {
}

async function onBeforeNavigate(details) {
if (!historyPermissionEnabled) {
return;
}
if (historyCleanUpQueue.includes(details.url)) {
return;
}
if (typeof details.url !== "string") {
return;
}
Expand All @@ -232,16 +274,26 @@ async function onBeforeNavigate(details) {
);
// in a container
if (container.name.startsWith("Temp")) {
if (!historyPermissionEnabled) {
return;
}
if (historyCleanUpQueue.includes(details.url)) {
return;
}
historyCleanUpQueue.push(details.url);
setToStorage("historyCleanUpQueue", historyCleanUpQueue);
}
} catch (e) {
// not in a container
const _isOnList = isOnRegexList(details.url);
if ((!mode && !_isOnList) || (mode && _isOnList)) {
await createTempContainerTab(details.url, true);
browser.tabs.remove(details.tabId);
}
}
}

function cleanupHistory() {
console.debug(historyCleanUpQueue);
const len = historyCleanUpQueue.length;

const its = len > 1 ? len / 2 : 1;
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
"tabs"
],
"optional_permissions": ["bookmarks", "history"],
"version": "1.1.22"
"version": "1.1.23"
}
84 changes: 62 additions & 22 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
<body>
<fieldset>
<span>
<input type="checkbox" id="opennewtab" /> toolbar button opens empty
temporary container
<input type="checkbox" id="opennewtab" /> check to make the toolbar button
open a empty new tab in a temporary container instead of opening the
active tab url in a new temporary container tab
</span>
</fieldset>

<fieldset>
Select colors to use for temporary containers
<abbr title="Hold CTRL to select or deselect multiple colors">(?)</abbr>
Select colors to use for temp containers (does not effect existing temp
containers)<br />
Hold CTRL to select or deselect multiple colors
<br />
<select id="usecolors" multiple style="min-height: 13em; width: 100%">
<option value="blue" selected>blue</option>
Expand All @@ -28,34 +30,72 @@
</fieldset>

<fieldset>
<b>Notes:</b>
<ul>
<b>Notes: </b>
<ol>
<li>
This addon has configurable shortcuts
<ol>
<li>Toolbar button action</li>
<li>Open newtab in temp container</li>
<li>Open a newtab in the same container as current tab</li>
<li>Open current tab url in multiple temp container</li>
</ol>
Grant the "Read and modify bookmarks" permission to enable the bookmark
context menu entry
</li>
<li>
To enable the bookmark context menu entry, grant the "Read and modify
bookmarks" permission
Grant the "Access browsing history" permission to enable automatic
history deletion for temp container tabs (experimental)
</li>
<li>
To enable automatic history deletion, grant the "Access browsing
history" permission
</li>
</ul>
</ol>
</fieldset>

<fieldset>
<span>
<input type="number" id="multiopen" value="3" min="0" max="10" />
Multi-Open parameter (Shortcut parameter)
<input type="number" id="multiopen" value="3" min="0" max="10" /> multi
open shortcut parameter
</span>
</fieldset>

<form id="mainForm" accept-charset="utf-8">
<fieldset>
<span
>Toggle between automatic(checked) and manual (unchecked) mode:
<input id="mode" type="checkbox" />
</span>
</fieldset>

<fieldset>
<h3 style="">URL RegEx List</h3>

<small>
<p>
in manual mode urls matching with any of these regexs will open in a
temp container, in automatic mode, any url matching with these regexs
will NOT open in a temp container, but everything else will
</p>
<b>Examples:</b>
<ul>
<li><code>^https:\/\/www\.youtube\.com.*</code>,</li>
<li>
<code>^https:\/\/www\.twitch\.tv.*</code>
</li>
</ul>
</small>
<input type="submit" value="Save list changes" />
<input
id="impbtn_wrapper"
type="button"
name="impbtn"
value="Import Rules"
/>
<input
id="impbtn"
type="file"
name="impbtn"
value=""
style="display: none"
/>
<input id="expbtn" type="button" name="impbtn" value="Export Rules" />

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

<script src="options.js"></script>
</body>
Loading

0 comments on commit 69510bf

Please sign in to comment.