forked from Spiderpowa/TJDict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
56 lines (48 loc) · 1.56 KB
/
content.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
function getSelected() {
var text;
if (window.getSelection) text = window.getSelection().toString();
else if(document.getSelection) text = document.getSelection();
else if(document.selection) text = document.selection.createRange().text;
return text;
}
function query(x, y){
var queryString = getSelected().trim();
if(queryString)
chrome.runtime.sendMessage({
q: queryString,
x: x, y: y
});
}
// 觸發事件 BEGIN
// Ctrl/Cmd + 滑鼠雙擊
window.addEventListener('dblclick', function(event){
if(event.metaKey || event.ctrlKey) query(event.screenX, event.screenY);
});
// Ctrl/Cmd + Alt + 滑鼠拖曳
window.addEventListener('mouseup', function(event){
if(event.altKey && (event.metaKey || event.ctrlKey)) query(event.screenX, event.screenY);
});
// 右鍵時記錄滑鼠位置
window.addEventListener('contextmenu', function(event){
chrome.storage.local.set({x: event.screenX, y: event.screenY});
});
// 按住一秒 BEGIN
var TIMEOUT_ID, HOLD;
window.addEventListener('mousedown', function(event){
chrome.storage.sync.get({hold_feature: false}, function(items){
if(items.hold_feature){
HOLD = true;
if(TIMEOUT_ID) clearTimeout(TIMEOUT_ID);
TIMEOUT_ID = setTimeout(function(){
if(HOLD) query(event.screenX, event.screenY);
HOLD = false;
}, 1000);
}
});
});
window.addEventListener('mouseup', function(event){HOLD = false;});
window.addEventListener('mousemove', function(event){
if(TIMEOUT_ID){clearTimeout(TIMEOUT_ID); TIMEOUT_ID = null;}
});
// 按住一秒 END
// 觸發事件 END