Skip to content

Commit

Permalink
fixes #223, fixes #225
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Neomy committed Apr 11, 2021
1 parent 426f998 commit 348cced
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 52 deletions.
3 changes: 3 additions & 0 deletions _locales/en/messages.json
Expand Up @@ -29,6 +29,9 @@
"options_favicon": {
"message": "Change favicon of discarded tabs (if possible)"
},
"options_prepends": {
"message": "Prepends a symbol to the discarded tabs (e.g. 💤 or ⏻︎) (if possible)"
},
"options_startup_unpinned": {
"message": "Discard all unpinned tabs on a browser or extension startup"
},
Expand Down
110 changes: 62 additions & 48 deletions background.js
@@ -1,10 +1,9 @@
/* globals hidden */
/* globals hidden, isFirefox */
'use strict';

const isFirefox = /Firefox/.test(navigator.userAgent);

const prefs = {
'favicon': true,
'prepends': '',
'number': 6,
'period': 10 * 60, // in seconds
'click': 'click.popup',
Expand Down Expand Up @@ -151,58 +150,73 @@ const discard = tab => new Promise(resolve => {
resolve();
};
// favicon
if (prefs.favicon) {
const src = tab.favIconUrl || '/data/page.png';
Object.assign(new Image(), {
crossOrigin: 'anonymous',
src,
onerror() {
next();
},
onload() {
const img = this;
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (ctx) {
canvas.width = img.width;
canvas.height = img.height;
const icon = () => {
if (prefs.favicon) {
const src = tab.favIconUrl || '/data/page.png';
Object.assign(new Image(), {
crossOrigin: 'anonymous',
src,
onerror() {
next();
},
onload() {
const img = this;
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (ctx) {
canvas.width = img.width;
canvas.height = img.height;

ctx.globalAlpha = 0.6;
ctx.drawImage(img, 0, 0);
ctx.globalAlpha = 0.6;
ctx.drawImage(img, 0, 0);

ctx.globalAlpha = 1;
ctx.beginPath();
ctx.fillStyle = '#a1a0a1';
ctx.arc(img.width * 0.75, img.height * 0.75, img.width * 0.25, 0, 2 * Math.PI, false);
ctx.fill();
const href = canvas.toDataURL('image/png');
ctx.globalAlpha = 1;
ctx.beginPath();
ctx.fillStyle = '#a1a0a1';
ctx.arc(img.width * 0.75, img.height * 0.75, img.width * 0.25, 0, 2 * Math.PI, false);
ctx.fill();
const href = canvas.toDataURL('image/png');

chrome.tabs.executeScript(tab.id, {
runAt: 'document_start',
allFrames: true,
matchAboutBlank: true,
code: `
window.stop();
if (window === window.top) {
[...document.querySelectorAll('link[rel*="icon"]')].forEach(link => link.remove());
chrome.tabs.executeScript(tab.id, {
runAt: 'document_start',
allFrames: true,
matchAboutBlank: true,
code: `
window.stop();
if (window === window.top) {
[...document.querySelectorAll('link[rel*="icon"]')].forEach(link => link.remove());
document.querySelector('head').appendChild(Object.assign(document.createElement('link'), {
rel: 'icon',
type: 'image/png',
href: '${href}'
}));
}
`
}, () => window.setTimeout(next, prefs['favicon-delay']) && chrome.runtime.lastError);
}
else {
next();
document.querySelector('head').appendChild(Object.assign(document.createElement('link'), {
rel: 'icon',
type: 'image/png',
href: '${href}'
}));
}
`
}, () => window.setTimeout(next, prefs['favicon-delay']) && chrome.runtime.lastError);
}
else {
next();
}
}
}
});
});
}
else {
next();
}
};
// change title
if (prefs.prepends) {
chrome.tabs.executeScript(tab.id, {
runAt: 'document_start',
code: `
window.stop();
document.title = '${prefs.prepends} ' + document.title;
`
}, icon);
}
else {
next();
icon();
}
});
discard.tabs = [];
Expand Down
5 changes: 5 additions & 0 deletions data/options/index.html
Expand Up @@ -49,6 +49,11 @@
<td><input type="checkbox" id="favicon"></td>
<td colspan="2"><label for="favicon" data-i18n="options_favicon"></label></td>
</tr>
<tr>
<td></td>
<td><label for="prepends" data-i18n="options_prepends"></label></td>
<td><input type="text" id="prepends"></td>
</tr>
<tr>
<td><input type="checkbox" id="startup-unpinned"></td>
<td colspan="2"><label for="startup-unpinned" data-i18n="options_startup_unpinned"></label></td>
Expand Down
10 changes: 7 additions & 3 deletions data/options/index.js
@@ -1,6 +1,7 @@
'use strict';

const isFirefox = /Firefox/.test(navigator.userAgent);
const isFirefox = /Firefox/.test(navigator.userAgent) || typeof InstallTrigger !== 'undefined';
const isEdge = /Edg\//.test(navigator.userAgent);

// localization
[...document.querySelectorAll('[data-i18n]')].forEach(e => {
Expand Down Expand Up @@ -51,6 +52,7 @@ const restore = () => storage({
'faqs': true,
'use-cache': false,
'favicon': true,
'prepends': '',
'go-hidden': false,
'memory-enabled': false,
'memory-value': 60,
Expand Down Expand Up @@ -80,6 +82,7 @@ const restore = () => storage({
document.getElementById('faqs').checked = prefs.faqs;
document.getElementById('use-cache').checked = prefs['use-cache'];
document.getElementById('favicon').checked = prefs.favicon;
document.getElementById('prepends').value = prefs.prepends;
document.getElementById('go-hidden').checked = prefs['go-hidden'];
if (prefs.period === 0) {
document.getElementById('period').value = 0;
Expand Down Expand Up @@ -166,6 +169,7 @@ document.getElementById('save').addEventListener('click', () => {
'faqs': document.getElementById('faqs').checked,
'use-cache': document.getElementById('use-cache').checked,
'favicon': document.getElementById('favicon').checked,
'prepends': document.getElementById('prepends').value,
'go-hidden': document.getElementById('go-hidden').checked,
'simultaneous-jobs': Math.max(1, Number(document.getElementById('simultaneous-jobs').value)),
'favicon-delay': Math.max(100, Number(document.getElementById('favicon-delay').value)),
Expand Down Expand Up @@ -242,10 +246,10 @@ document.getElementById('reset').addEventListener('click', e => {
}
});
// rate
if (/Firefox/.test(navigator.userAgent)) {
if (isFirefox) {
document.getElementById('rate').href = 'https://addons.mozilla.org/firefox/addon/auto-tab-discard/reviews/';
}
else if (/Edg\//.test(navigator.userAgent)) {
else if (isEdge) {
document.getElementById('rate').href = 'https://microsoftedge.microsoft.com/addons/detail/nfkkljlcjnkngcmdpcammanncbhkndfe';
}
// export
Expand Down
5 changes: 4 additions & 1 deletion firefox.js
@@ -1,7 +1,10 @@
/*
add "autoDiscardable" support to "chrome.tabs.query" and "chrome.tabs.update"
*/
if (/Firefox/.test(navigator.userAgent)) {

const isFirefox = /Firefox/.test(navigator.userAgent) || typeof InstallTrigger !== 'undefined';

if (isFirefox) {
const cache = {};
const query = chrome.tabs.query;
chrome.tabs.query = function(queryInfo, callback = () => {}) {
Expand Down

0 comments on commit 348cced

Please sign in to comment.