-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js~
131 lines (108 loc) · 3.37 KB
/
popup.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function buttonElement(targetId) {
return document.getElementById(targetId);
}
function setButtonImage(targetId, targetUrl) {
buttonElement(targetId).style.backgroundImage = `url(${targetUrl})`;
return
}
function addClickEvent(func, targetId, targetUrl, type) {
func(targetId).addEventListener('click', () => {
let stopButton = buttonElement('stop');
if (stopButton.innerHTML === 'START Extension') {
stopButton.innerHTML= 'STOP Extension'};
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
chrome.tabs.sendMessage(
tabs[0].id,
{
name: targetId,
path: targetUrl,
type: type,
eventName: func(targetId).className ,
isItOn: true,
sender: 'popup'}
);
chrome.runtime.onMessage.addListener((msg, _, sendRes) => {
chrome.action.setIcon({path: targetUrl,
tabId: tabs[0].id
});
});
});
});
return;
}
function startPointerFunction(targetId, targetUrl, type) {
buttonElement(targetId);
setButtonImage(targetId, targetUrl);
addClickEvent(buttonElement, targetId, targetUrl, type);
return;
}
function triggerStop(func, targetId) {
func(targetId).addEventListener('click', () => {
let buttonName = ''
if (targetId === 'stop') buttonName = stopEvent(func, targetId);
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
chrome.storage.local.get(['msg'], function(res){
console.log(res)
console.log(targetId)
chrome.tabs.sendMessage(
tabs[0].id,
{
name: res.msg.name,
path: res.msg.path,
type: buttonName !== 'STOP Extension' ? targetId : 'moving',
sender: 'popup'}
);
})
});
});
}
function stopEvent(func, targetId) {
let stopButton = func(targetId);
stopButton.innerHTML === 'STOP Extension' ?
stopButton.innerHTML = 'START Extension' :
stopButton.innerHTML = 'STOP Extension';
return stopButton.innerHTML;
}
chrome.storage.local.get(['advertised'], function(res1) {
chrome.storage.local.get(['stamp'], function(res) {
console.log(res)
let stamps = Object.keys(res.stamp)
, advertised = res1.advertised
console.log(stamps)
console.log(advertised)
if (stamps.length < 3 && !advertised) {
let lastStamp = stamps[stamps.length -1]
let aDay = 1000 * 60 * 60 * 24
today = new Date()
console.log(lastStamp)
lastStamp = Date.parse(lastStamp)
console.log(lastStamp)
let aDayOver = lastStamp + aDay > today || isNaN(lastStamp)
let stampObj = isNaN(lastStamp) ? {stamp : {}} : res
console.log(aDayOver
if (aDayOver) {
stampObj.stamp[today] = 1
console.log(stampObj)
chrome.storage.local.set(stampObj)
}
} else {
chrome.storage.local.set({advertised: true})
}
return
})
})
let targets = [ // add new feature name here
'bubble', 'letter', 'snowflake', 'heart', 'heart1',
'colorfulBall', 'strawblueberry', 'bunny', 'dna', 'card',
'atom', 'petal', 'float', 'springCooler', 'colorSpring',
'coins', 'digital', 'particleMagnet', 'test'
]
for (let i = 0; i < targets.length; i++ ){
let path = `./images/${targets[i]}/${targets[i]}32.png`
startPointerFunction(targets[i], path, 'moving');
}
triggerStop(buttonElement, 'stop');
chrome.storage.local.get(['msg'], function(res){
if(res.msg.type === 'stop') buttonElement('stop').innerHTML = 'START Extension';
console.log(res)
})