diff --git a/background.js b/background.js index 9f59644..62b00bf 100644 --- a/background.js +++ b/background.js @@ -4,40 +4,44 @@ * @date 2020/11/04 17:54:49 */ -chrome.browserAction.onClicked.addListener(function(ctab) { - var cid = ctab.id - var tid = null - var code = ` - var script = document.createElement('script') - script.textContent = \` - chrome.send('clearHostResolverCache') - chrome.send('flushSocketPools') - \` - document.body.appendChild(script) - ` - var callback = function() { - chrome.tabs.remove(tid) - chrome.tabs.executeScript(cid, { code: 'location.reload()' }) - } +chrome.runtime.onInstalled.addListener(() => { + chrome.action.onClicked.addListener(function (ctab) { + var cid = ctab.id + var tid = null - 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.tabs.executeScript(tab.id, { code }, callback) - done = true - break - } + var callback = function () { + chrome.tabs.remove(tid) + chrome.scripting.executeScript({ + target: { tabId: cid }, + files: ['done.js'] + }) } - if (!done) { - chrome.tabs.create( - { url: 'chrome://net-internals/#dns', active: false }, - function(tab) { + + 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.tabs.executeScript(tab.id, { code }, callback) + 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 + ) + } + ) + } + }) }) }) diff --git a/clean.js b/clean.js new file mode 100644 index 0000000..5b8ec96 --- /dev/null +++ b/clean.js @@ -0,0 +1,6 @@ +console.log(chrome) +// chrome.send('clearHostResolverCache') +// chrome.send('flushSocketPools') + +window['dns-view-clear-cache'].click() +window['sockets-view-flush-button'].click() diff --git a/done.js b/done.js new file mode 100644 index 0000000..3e5ecc1 --- /dev/null +++ b/done.js @@ -0,0 +1 @@ +location.reload() diff --git a/icon128.png b/icon128.png deleted file mode 100644 index 2c4f709..0000000 Binary files a/icon128.png and /dev/null differ diff --git a/icon256.png b/icon256.png deleted file mode 100644 index 8a36859..0000000 Binary files a/icon256.png and /dev/null differ diff --git a/icon64.png b/icon64.png deleted file mode 100644 index 5ecf9fc..0000000 Binary files a/icon64.png and /dev/null differ diff --git a/icons/128.png b/icons/128.png new file mode 100644 index 0000000..16b3e51 Binary files /dev/null and b/icons/128.png differ diff --git a/icons/256.png b/icons/256.png new file mode 100644 index 0000000..f6857f0 Binary files /dev/null and b/icons/256.png differ diff --git a/icons/64.png b/icons/64.png new file mode 100644 index 0000000..7e6e539 Binary files /dev/null and b/icons/64.png differ diff --git a/manifest.json b/manifest.json index e917698..b1bd650 100644 --- a/manifest.json +++ b/manifest.json @@ -1,24 +1,24 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "dns-killer", "description": "DNS缓存杀手,一键清除chrome的DNS缓存, 并刷新当前选项卡。 web开发的利器。", - "version": "1.0.0", + "version": "2.0.0", "author": "yutent", "icons": { - "64": "icon64.png", - "128": "icon128.png", - "256": "icon256.png" + "64": "icons/64.png", + "128": "icons/128.png", + "256": "icons/256.png" }, "background": { - "scripts": ["background.js"] + "service_worker": "background.js", + "type": "module" }, - "browser_action": { - "default_icon": "icon64.png" + "action": { + "default_icon": { "64": "icons/64.png", "128": "icons/128.png" }, + "default_title": "点击清除DNS缓存" }, - "permissions": [ - "tabs", - "chrome://net-internals/#dns", - "chrome://extensions/" - ], - "homepage_url": "https://yutent.top" + "permissions": ["tabs", "activeTab", "scripting"], + "host_permissions": ["chrome://net-internals/#dns"], + "optional_host_permissions": ["chrome://extensions/"], + "homepage_url": "https://github.com/yutent" }