-
Notifications
You must be signed in to change notification settings - Fork 190
/
SidebarGestures.uc.js
163 lines (161 loc) · 7.27 KB
/
SidebarGestures.uc.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
location == "chrome://browser/content/browser.xul" && (function () {
ucjsSidebarGestures = {
lastX: 0,
lastY: 0,
sourceNode: "",
directionChain: "",
isMouseDownL: false,
isMouseDownR: false,
hideFireContext: false,
shouldFireContext: false,
GESTURES: {
//后退
"L": {
name: "\u540e\u9000",
cmd: function() {
var win = ucjsSidebarGestures.getSidebarWindow();
win.history.back();
}
},
//前进
"R": {
name: "\u524d\u8fdb",
cmd: function() {
var win = ucjsSidebarGestures.getSidebarWindow();
win.history.forward();
}
},
//刷新当前页面
"UD": {
name: "\u5237\u65b0\u5f53\u524d\u9875\u9762",
cmd: function() {
var win = ucjsSidebarGestures.getSidebarWindow();
win.location.reload();
}
},
},
init: function (win) {
var self = this;
["mousedown", "mousemove", "mouseup", "contextmenu", "DOMMouseScroll", "dragend"].forEach(function (type) {
win.addEventListener(type, self, true);
});
win.addEventListener("unload", function () {
["mousedown", "mousemove", "mouseup", "contextmenu", "DOMMouseScroll", "dragend"].forEach(function (type) {
win.removeEventListener(type, self, true);
});
}, false);
},
handleEvent: function (event) {
switch (event.type) {
case "mousedown":
if(/object|embed/i.test(event.target.localName)) return;
if (event.button == 2) {
this.sourceNode = event.target;
this.isMouseDownR = true;
this.hideFireContext = false;
[this.lastX, this.lastY, this.directionChain] = [event.screenX, event.screenY, ""];
}
if (event.button == 2 && this.isMouseDownL) {
this.isMouseDownR = false;
this.shouldFireContext = false;
this.hideFireContext = true;
this.directionChain = "L>R";
this.stopGesture(event);
} else if (event.button == 0) {
this.isMouseDownL = true;
if (this.isMouseDownR) {
this.isMouseDownL = false;
this.shouldFireContext = false;
this.hideFireContext = true;
this.directionChain = "L<R";
this.stopGesture(event);
}
}
break;
case "mousemove":
if (this.isMouseDownR) {
this.hideFireContext = true;
var [subX, subY] = [event.screenX - this.lastX, event.screenY - this.lastY];
var [distX, distY] = [(subX > 0 ? subX : (-subX)), (subY > 0 ? subY : (-subY))];
var direction;
if (distX < 10 && distY < 10) return;
if (distX > distY) direction = subX < 0 ? "L" : "R";
else direction = subY < 0 ? "U" : "D";
if (direction != this.directionChain.charAt(this.directionChain.length - 1)) {
this.directionChain += direction;
XULBrowserWindow.statusTextField.label = this.GESTURES[this.directionChain] ? "\u624b\u52bf: " + this.directionChain + " " + this.GESTURES[this.directionChain].name : "\u672a\u77e5\u624b\u52bf:" + this.directionChain;
}
this.lastX = event.screenX;
this.lastY = event.screenY;
}
break;
case "mouseup":
if (event.ctrlKey && event.button == 2) {
this.isMouseDownL = false;
this.isMouseDownR = false;
this.shouldFireContext = false;
this.hideFireContext = false;
this.directionChain = "";
event.preventDefault();
XULBrowserWindow.statusTextField.label = "\u53d6\u6d88\u624b\u52bf";
break;
}
if (this.isMouseDownR && event.button == 2) {
if (this.directionChain) this.shouldFireContext = false;
this.isMouseDownR = false;
this.directionChain && this.stopGesture(event);
} else if (event.button == 0 && this.isMouseDownL) {
this.isMouseDownL = false;
this.shouldFireContext = false;
}
break;
case "contextmenu":
if (this.isMouseDownL || this.isMouseDownR || this.hideFireContext) {
var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
var contextmenu = pref.getBoolPref("dom.event.contextmenu.enabled");
pref.setBoolPref("dom.event.contextmenu.enabled", true);
setTimeout(function () {
pref.setBoolPref("dom.event.contextmenu.enabled", contextmenu);
}, 10);
event.preventDefault();
event.stopPropagation();
this.shouldFireContext = true;
this.hideFireContext = false;
}
break;
case "DOMMouseScroll":
if (this.isMouseDownR) {
event.preventDefault();
event.stopPropagation();
this.shouldFireContext = false;
this.hideFireContext = true;
this.directionChain = "W" + (event.detail > 0 ? "+" : "-");
this.stopGesture(event);
}
break;
case "dragend":
this.isMouseDownL = false;
}
},
stopGesture: function (event) {
(this.GESTURES[this.directionChain] ? this.GESTURES[this.directionChain].cmd(this, event) & (XULBrowserWindow.statusTextField.label = "") : (XULBrowserWindow.statusTextField.label = "\u672a\u77e5\u624b\u52bf:" + this.directionChain)) & (this.directionChain = "");
},
getSidebarWindow: function(){
var sidebar = document.getElementById('sidebar');
var webPanel = sidebar.contentDocument.getElementById("web-panels-browser");
if(webPanel){
return webPanel.contentWindow;
}
}
};
var sidebar = document.getElementById('sidebar');
sidebar.addEventListener('DOMContentLoaded', function(){
if (sidebar.contentDocument){
sidebar.removeEventListener('DOMContentLoaded', arguments.callee, false);
var wpb = sidebar.contentDocument.getElementById('web-panels-browser');
if (wpb) {
ucjsSidebarGestures.init(sidebar.contentWindow);
}
}
}, false);
})()