-
Notifications
You must be signed in to change notification settings - Fork 31
/
background.js
60 lines (52 loc) · 1.7 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
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({
name: "Jack"
});
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && /^http/.test(tab.url)) {
chrome.scripting.insertCSS({
target: { tabId: tabId },
files: ["./foreground_styles.css"]
})
.then(() => {
console.log("INJECTED THE FOREGROUND STYLES.");
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ["./foreground.js"]
})
.then(() => {
console.log("INJECTED THE FOREGROUND SCRIPT.");
});
})
.catch(err => console.log(err));
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'get_name') {
chrome.storage.local.get('name', data => {
if (chrome.runtime.lastError) {
sendResponse({
message: 'fail'
});
return;
}
sendResponse({
message: 'success',
payload: data.name
});
});
return true;
} else if (request.message === 'change_name') {
chrome.storage.local.set({
name: request.payload
}, () => {
if (chrome.runtime.lastError) {
sendResponse({ message: 'fail' });
return;
}
sendResponse({ message: 'success' });
})
return true;
}
});