dns-killer/background.js

44 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-07-07 21:05:26 +08:00
/**
2020-11-04 17:59:31 +08:00
* DNS缓存杀手
* @author yutent<yutent.io@gmail.com>
* @date 2020/11/04 17:54:49
2018-07-07 21:05:26 +08:00
*/
2018-07-29 14:09:34 +08:00
chrome.browserAction.onClicked.addListener(function(ctab) {
2020-11-04 17:59:31 +08:00
var cid = ctab.id
var tid = null
var code = `
var script = document.createElement('script')
2018-07-29 14:09:34 +08:00
script.textContent = \`
chrome.send('clearHostResolverCache')
chrome.send('flushSocketPools')
\`
document.body.appendChild(script)
`
2020-11-04 17:59:31 +08:00
var callback = function() {
2018-07-29 14:09:34 +08:00
chrome.tabs.remove(tid)
chrome.tabs.executeScript(cid, { code: 'location.reload()' })
}
chrome.tabs.query({}, function(tabs) {
2020-11-04 17:59:31 +08:00
var done = false
2018-07-29 14:09:34 +08:00
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
}
}
if (!done) {
chrome.tabs.create(
{ url: 'chrome://net-internals/#dns', active: false },
function(tab) {
tid = tab.id
chrome.tabs.executeScript(tab.id, { code }, callback)
}
)
}
})
2018-07-07 21:05:26 +08:00
})