-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
48 lines (42 loc) · 1.3 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let chromeNetInternalTabId = null
const chromeNetInternalsUrl = 'chrome://net-internals/#dns'
const EVENT_STRATEGIES = {
'DNSFlushScriptLoaded': function DNSFlushScriptLoadedHandler () {
chrome.tabs.sendMessage(chromeNetInternalTabId, { action: 'DO_FLUSH' })
},
'DNSFlushCompleted': function DNSFlushCompletedHandler () {
sendSuccessNotification('Congrats, DNS Flushed')
closeTab(chromeNetInternalTabId)
},
'DNSFlushFailed': function DNSFlushFailedHandler () {
sendSuccessNotification('Sorry, can not Flush DNS as expected')
closeTab(chromeNetInternalTabId)
}
}
chrome.browserAction.onClicked.addListener(() => {
openTab(chromeNetInternalsUrl)
})
chrome.runtime.onConnect.addListener(portFrom => {
if(portFrom.name === 'content-script') {
portFrom.onMessage.addListener((data = { event: 'none' }) => {
const handler = EVENT_STRATEGIES[data.event]
if (handler) handler.call(null)
})
}
})
function openTab (url = '') {
chrome.tabs.create({ url }, tab => {
chromeNetInternalTabId = tab.id
})
}
function closeTab (id = null) {
if (id !== null) chrome.tabs.remove(id)
}
function sendSuccessNotification (message) {
chrome.notifications.create({
type: 'basic',
title: 'Flush DNS Notification',
message,
iconUrl: 'images/icon-128px.png'
})
}