forked from marani/midas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm.popup.js
143 lines (118 loc) · 4.22 KB
/
mm.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
132
133
134
135
136
137
138
139
140
141
142
143
window.onload = function(){
// a pop up is raised means user has clicked or invoked this browser action
// -> state change
//remember to set permission to executeScript content_script from popup
//OS Hotfix
if (navigator.appVersion.indexOf("Win")!=-1) var OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) var OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) var OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) var OSName="Linux";
var defaultSetting = {
state: "activated",
style: {
showHand: "true"
},
scroll: {
reverse: "yes",
slide: "yes",
scale: "1.5"
},
activation: {
mouse: (OSName == "Linux" || OSName == "MacOS") ? "2" : "3",
key: []
}
};
function notifyAndClose(){
document.getElementById("status").innerHTML = "Hand tool " + setting.state;
document.getElementById("status").style.color = (setting.state=="activated")?"#19B843":"#000";
setTimeout(function(){
finished["setStatus"] = true;
closeIfFinish();
}, 1250);
}
function closeIfFinish(){
for (var i in finished){
if (!finished[i]) return;
}
//otherwise, all are finished
window.close();
}
function setLocalSetting(){
}
function getLocalSetting(){
//get setting from local Storage
//return as object
//if local storage empty, return default
var settingStr = localStorage["setting"];
var setting;
if (!settingStr)
setting = defaultSetting;
else
setting = JSON.parse(settingStr);
return setting;
}
function deactivate(){
setting.state = "deactivated";
//notify script in page to disable
chrome.tabs.executeScript({
code: "window.postMessage({setting: JSON.parse('" + JSON.stringify(setting) + "'), type: 'mm.popup.notify'},'*');"
});
//set storage to new value
console.log('setting done');
localStorage["setting"] = JSON.stringify(setting);
chrome.browserAction.setTitle({title: "Hand tool deactivated"});
chrome.browserAction.setIcon({path: "default-pointer.png"}, function(){
finished["setIcon"] = true;
//finish icon change
closeIfFinish();
});
//finish setting
notifyAndClose();
}
function activate(){
setting.state = "activated";
//notify script in page to disable
chrome.tabs.executeScript({
code: "window.postMessage({setting: JSON.parse('" + JSON.stringify(setting) + "'), type: 'mm.popup.notify'},'*');"
});
//set storage to new value
console.log('setting done');
localStorage["setting"] = JSON.stringify(setting);
//ugly unscalable code
var activation = "";
if (setting.activation.key.length) {
if (setting.activation.key[0] == "ctrlKey")
activation = "Control";
if (setting.activation.mouse == "2")
activation += " + Middle Mouse";
else
if (setting.activation.mouse == "3")
activation += " + Right Mouse";
}
else {
if (setting.activation.mouse == "2")
activation = "Middle Mouse";
else
if (setting.activation.mouse == "3")
activation = "Right Mouse";
}
//end ugly unscalable code
chrome.browserAction.setTitle({title: "Hand tool activated (" + activation + ")"});
chrome.browserAction.setIcon({path: "hand-pointer.png"}, function(){
finished["setIcon"] = true;
//finish icon change
closeIfFinish();
});
//finish setting
notifyAndClose();
}
var setting = getLocalSetting();
var finished = {
setIcon: false,
setStatus: false
};
if (setting.state == "activated")
deactivate();
else
activate();
};