-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
112 lines (95 loc) · 2.91 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
chrome.runtime.onInstalled.addListener(() => {
chrome.declarativeContent.onPageChanged.removeRules(undefined, () => {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [new chrome.declarativeContent.PageStateMatcher({
})
],
actions: [new chrome.declarativeContent.ShowPageAction()]
}])
return
});
chrome.contextMenus.create({
"id": "tails-mouse-footpring-switch",
"title": "STOP Extension"
});
return
});
chrome.contextMenus.onClicked.addListener(() => {
chrome.storage.local.get(['msg'], function(res){
if(res.msg.type === 'stop') {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
chrome.tabs.sendMessage(
tabs[0].id,
{
name: res.msg.name,
path: res.msg.name,
type: 'moving'})
return
});
} else {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
chrome.tabs.sendMessage(
tabs[0].id,
{
type: 'stop',
name: res.msg.name,
path: res.msg.name
})
return
});
}
return
})
})
chrome.runtime.onMessage.addListener((msg) => {
const stopExtension = chrome.i18n.getMessage('stopExtension')
const startExtension = chrome.i18n.getMessage('startExtension')
let msgObj;
if (msg.type === 'moving') {
msgObj = {
name: msg.name,
path: msg.path,
type: 'check'
}
chrome.storage.local.set({msg: msgObj})
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
chrome.scripting.executeScript(
{ target: {tabId: tabs[0].id},
files: ['./functions/' + msg.name + '.js'] });
return
});
chrome.contextMenus.update( "tails-mouse-footpring-switch", {"title": stopExtension});
} else if (msg.type === 'stop') {
msgObj = {
type: 'stop',
name: msg.name,
path: msg.path
}
chrome.storage.local.set({msg: msgObj})
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
chrome.scripting.executeScript(
{ target: { tabId : tabs[0].id},
files: ['./functions/' + 'stop' + '.js'] });
return
});
chrome.contextMenus.update( "tails-mouse-footpring-switch", {"title": startExtension});
} else if (msg.type === 'check') {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
chrome.storage.local.get(['msg'], function(res){
// console.log(res)
if (Object.keys(res).length === 0) return;
chrome.tabs.sendMessage(
tabs[0].id,
{ name: res.msg.name,
path: res.msg.path,
type: res.msg.type === 'stop' ? 'stop' : 'moving',
sender: 'background'}
);
chrome.action.setIcon({
path: res.msg.path,
tabId: tabs[0].id});
})
});
}
return
})