diff --git a/background.js b/background.js index 62b00bf..4f60930 100644 --- a/background.js +++ b/background.js @@ -4,44 +4,42 @@ * @date 2020/11/04 17:54:49 */ -chrome.runtime.onInstalled.addListener(() => { - chrome.action.onClicked.addListener(function (ctab) { - var cid = ctab.id - var tid = null +chrome.action.onClicked.addListener(function (ctab) { + var cid = ctab.id + var tid = null - var callback = function () { - chrome.tabs.remove(tid) - chrome.scripting.executeScript({ - target: { tabId: cid }, - files: ['done.js'] - }) + var callback = function () { + chrome.tabs.remove(tid) + chrome.scripting.executeScript({ + target: { tabId: cid }, + files: ['done.js'] + }) + } + + chrome.tabs.query({}, function (tabs) { + var done = false + for (let i = 0, tab; (tab = tabs[i++]); ) { + if (/^chrome:\/\/net-internals/.test(tab.url)) { + tid = tab.id + chrome.scripting.executeScript( + { target: { tabId: tab.id }, files: ['clean.js'] }, + callback + ) + done = true + break + } } - - chrome.tabs.query({}, function (tabs) { - var done = false - for (let i = 0, tab; (tab = tabs[i++]); ) { - if (/^chrome:\/\/net-internals/.test(tab.url)) { + if (!done) { + chrome.tabs.create( + { url: 'chrome://net-internals/#dns', active: false }, + function (tab) { tid = tab.id chrome.scripting.executeScript( { target: { tabId: tab.id }, files: ['clean.js'] }, callback ) - done = true - break } - } - if (!done) { - chrome.tabs.create( - { url: 'chrome://net-internals/#dns', active: false }, - function (tab) { - tid = tab.id - chrome.scripting.executeScript( - { target: { tabId: tab.id }, files: ['clean.js'] }, - callback - ) - } - ) - } - }) + ) + } }) })