-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_script.js
35 lines (31 loc) · 1.19 KB
/
event_script.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
function setIconDisabled (disabled) {
chrome.browserAction.setIcon({
path: `icons/icon16${disabled ? '_disabled' : ''}.png`
});
}
function getCredential (tab, host) {
let
credentials = JSON.parse(localStorage.getItem('credentials')),
hostname = tab ? tab.url.replace(/^(.*:)\/\/([A-Za-z0-9\-\.]+)(:[0-9]+)?(.*)$/, '$2') : host,
credential = credentials.filter(item => item.host === hostname)[0];
return credential;
}
chrome.runtime.onInstalled.addListener(function () {
let credentials = localStorage.getItem('credentials') || JSON.stringify([]);
localStorage.setItem('credentials', credentials);
});
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.getCredentialsForHost !== undefined) {
let credential = getCredential(null, message.getCredentialsForHost);
setIconDisabled(!credential);
sendResponse({
message: credential,
sender: "event_script.js"
});
}
});
chrome.tabs.onActivated.addListener(function () {
chrome.tabs.getSelected(null, function (tab) {
setIconDisabled(!getCredential(tab));
});
});