forked from jialongl/WebsiteIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
78 lines (68 loc) · 1.55 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Add icon to URL bar
function checkForValidUrl(tabId, changeInfo, tab) {
chrome.pageAction.show(tab.id);
};
// Listen for any changes to the URL of any tab
chrome.tabs.onUpdated.addListener(checkForValidUrl);
// Set the item in the localstorage
function setItem(key, value) {
window.localStorage.removeItem(key);
window.localStorage.setItem(key, value);
}
// Get the item from local storage with the specified key
function getItem(key) {
var value;
try {
value = window.localStorage.getItem(key);
}catch(e) {
value = "null";
}
return value;
}
// get IP using webRequest
var currentIPList = {};
chrome.webRequest.onCompleted.addListener(
function(info) {
currentIPList[ info.url ] = info.ip;
return;
},
{
urls: [],
types: []
},
[]
);
// Listeners
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse)
{
switch (request.name)
{
case "setOptions":
// request from the content script to set the options.
//localStorage["websiteIP_status"] = websiteIP_status;
localStorage.setItem("websiteIP_status", request.status);
break;
case "getOptions":
// request from the content script to get the options.
sendResponse({
enableDisableIP : localStorage["websiteIP_status"]
});
break;
case "getIP":
var currentURL = sender.tab.url;
if (currentIPList[currentURL] !== undefined) {
sendResponse({
domainToIP: currentIPList[currentURL]
});
} else {
sendResponse({
domainToIP: null
});
}
break;
default:
sendResponse({});
}
}
);