Skip to content

Commit

Permalink
Update reload tab logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jc3213 committed Jun 3, 2024
1 parent ee49077 commit 9b23d88
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
30 changes: 18 additions & 12 deletions mv2/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var easyStorage = {};
var easyPAC = '';
var easyPACX = '';
var easyMatch = {};
var easyMatchLog = {};
var easyTempo = {};
var easyTempoLog = {};

Expand All @@ -24,7 +23,7 @@ chrome.runtime.onMessage.addListener(({action, params}, sender, response) => {
easyTempoProxy(params);
break;
case 'easyproxy_purgetempo':
easyTempoPurge();
easyTempoPurge(params);
}
});

Expand All @@ -36,21 +35,22 @@ function easyPluginInit(response) {
});
}

function easyOptionsChanges({storage, removed}, response) {
function easyOptionsChanges({storage, removed, tabId}, response) {
easyStorage = storage;
pacScriptConverter();
response({pac_script: easyPAC});
chrome.storage.local.set(storage);
if (removed?.length > 0) {
chrome.storage.local.remove(removed);
}
easyReloadTab(tabId);
}

function easyToolbarQuery(tabId, response) {
response(easyMatch[tabId]);
response(easyMatch[tabId].list);
}

function easyTempoProxy({proxy, include, exclude}) {
function easyTempoProxy({tabId, proxy, include, exclude}) {
if (!easyTempo[proxy]) {
easyTempo[proxy] = [];
easyTempoLog[proxy] = {};
Expand All @@ -69,34 +69,40 @@ function easyTempoProxy({proxy, include, exclude}) {
console.log('Proxy Server: ' + proxy + '\nAdded Tempo: ' + result.join(' ') + '\nRemoved Tempo: ' + exclude.join(' '));
tempo.push(...result);
pacScriptConverter();
easyReloadTab(tabId);
}

function easyTempoPurge() {
function easyTempoPurge({tabId}) {
easyTempo = {};
easyTempoLog = {};
pacScriptConverter();
easyReloadTab(tabId);
}

function easyReloadTab(id) {
if (id) {
chrome.tabs.update(id, { url: easyMatch[id].url });
}
}

chrome.webNavigation.onBeforeNavigate.addListener(({tabId, url, frameId}) => {
if (frameId === 0) {
var pattern = easyMatchPattern(url);
easyMatch[tabId] = [pattern];
easyMatchLog[tabId] = {[pattern]: true};
easyMatch[tabId] = { list: [pattern], rule: { [pattern]: true }, url};
}
});

chrome.webRequest.onBeforeRequest.addListener(({tabId, url}) => {
var pattern = easyMatchPattern(url);
if (!pattern || !easyMatchLog[tabId] || easyMatchLog[tabId][pattern]) {
if (!pattern || !easyMatch[tabId]?.rule || easyMatch[tabId].rule[pattern]) {
return;
}
easyMatch[tabId].push(pattern);
easyMatchLog[tabId][pattern] = true;
easyMatch[tabId].list.push(pattern);
easyMatch[tabId].rule[pattern] = true;
}, {urls: ['http://*/*', 'https://*/*']});

chrome.tabs.onRemoved.addListener(({tabId}) => {
delete easyMatch[tabId];
delete easyMatchLog[tabId];
});

chrome.storage.local.get(null, (json) => {
Expand Down
9 changes: 3 additions & 6 deletions mv2/pages/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ function proxySubmit() {
var proxy = proxies.value;
var {include, exclude, manage} = proxyChange('match', proxy, easyStorage[proxy], easyMatch);
if (manage) {
chrome.runtime.sendMessage({action: 'options_onchange', params: {storage: easyStorage}},
() => chrome.tabs.reload(easyId));
chrome.runtime.sendMessage({action: 'options_onchange', params: {storage: easyStorage, tabId: easyId}});
}
}

Expand All @@ -53,8 +52,7 @@ function proxyTempo(remove) {
}
var {include, exclude, manage} = proxyChange('tempo', proxy, easyTempo[proxy], easyMatchTempo);
if (manage) {
chrome.runtime.sendMessage({action: 'easyproxy_changetempo', params: {proxy, include, exclude}},
() => chrome.tabs.reload(easyId));
chrome.runtime.sendMessage({action: 'easyproxy_changetempo', params: {tabId: easyId, proxy, include, exclude}});
}
}

Expand All @@ -65,8 +63,7 @@ function proxyTempoPurge(proxy) {
match.parentNode.classList.remove('tempo');
match.checked = easyMatch[match.value] === proxy || easyMatchTempo[match.value] === proxy ? true : false;
});
chrome.runtime.sendMessage({action: 'easyproxy_purgetempo'},
() => chrome.tabs.reload(easyId));
chrome.runtime.sendMessage({action: 'easyproxy_purgetempo', params: {tabId: easyId}});
}

function proxyChange(type, proxy, storage, logs) {
Expand Down

0 comments on commit 9b23d88

Please sign in to comment.