forked from ywzhaiqi/userChromeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnapLinksMod.uc.xul
466 lines (430 loc) · 15.3 KB
/
SnapLinksMod.uc.xul
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?xml version="1.0"?>
<!DOCTYPE overlay>
<!--
// ==UserScript==
// @name SnapLinksMod.uc.xul
// @namespace http://d.hatena.ne.jp/Griever/
// @description Snap Links for userChromeJS
// @include main
// @charset UTF-8
// @version 2012/08/22
// @homepageURL https://github.com/ywzhaiqi/userChromeJS/blob/master/SnapLinksMod.uc.xul
// @note 2012/08/22 add copy links Reverse by ywzhaiqi
// @note 2012/06/28 add radiobox options.
// @note 2012/06/01 hide entry menu on link, image, text input area or context selected.
// @note 2012/04/02 fixed by lastdream2013, add display count, fix open/copy url order, add context menu and fix memory leak
// @note CSS が適用されていなかったのを修正
// @note 2012/03/08 フレーム跨いだりツールチップ使うのをやめた
// ==/UserScript==
-->
<!--
FireGestures や keyconfig で snapLinks(); を実行
引数に eventを送るとすぐに四角を描くようになる(MouseEvent限定)
snapLinks.init();
-->
<?xml-stylesheet href="data:text/css;charset=utf-8,@namespace%20url('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul');
%23snapLinksMenupopup.hasLink .hasLink,
%23snapLinksMenupopup.hasImageLink .hasImageLink,
%23snapLinksMenupopup.hasCheckbox .hasCheckbox,
%23snapLinksMenupopup.hasRadio .hasRadio
{ display: -moz-box !important; }
%23snapLinksMenupopup:not(.hasLink) .hasLink,
%23snapLinksMenupopup:not(.hasImageLink) .hasImageLink,
%23snapLinksMenupopup:not(.hasCheckbox) .hasCheckbox,
%23snapLinksMenupopup:not(.hasRadio) .hasRadio
{ display: none !important; }
" type="text/css"?>
<overlay id="SnapLinks_overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<popupset id="mainPopupSet">
<menupopup id="snapLinksMenupopup"
onpopuphidden="this.className = ''; snapLinks.lowlightAll();">
<menuitem label="在新标签打开所有链接"
id="SnapLinksOpenLinks"
class="hasLink"
oncommand="snapLinks.openLinks();" />
<menuitem label="复制所有链接URL"
id="SnapLinksCopyLinks"
class="hasLink"
oncommand="snapLinks.copyLinks();" />
<menuitem label="复制所有链接URL(反向)"
id="SnapLinksCopyLinksReverse"
class="hasLink"
oncommand="snapLinks.copyLinks(null, true);" />
<menuitem label="在新标签页打开所有图片链接" hidden="true"
id="SnapLinksOpenImageLinks"
class="hasImageLink"
oncommand="snapLinks.openLinks(/\.(jpe?g|png|gif|bmp)$/i);" />
<menuitem label="在一个标签页显示所有图片链接"
id="SnapLinksImageLinksOnePage"
class="hasImageLink"
oncommand="snapLinks.imageOnePage();" />
<menuseparator class="hasLink hasCheckbox hasRadio" />
<menuitem label="复选框 - 选中"
id="SnapLinksCheckBoxSelect"
class="hasCheckbox"
oncommand="snapLinks.checkbox(true);" />
<menuitem label="复选框 - 取消"
id="SnapLinksCheckBoxCancel"
class="hasCheckbox"
oncommand="snapLinks.checkbox(false);" />
<menuitem label="复选框 - 反选"
id="SnapLinksCheckBoxTaggle"
class="hasCheckbox"
oncommand="snapLinks.checkbox();" />
<menuitem label="单选框 - 选中"
id="SnapLinksRadioSelect"
class="hasRadio"
oncommand="snapLinks.radio(true);" />
<menuitem label="单选框 - 取消"
id="SnapLinksRadioCancel"
class="hasRadio"
oncommand="snapLinks.radio(false);" />
</menupopup>
<popup id="contentAreaContextMenu">
<menuitem id="snaplinksMenuEntry"
label="snaplinks批量操作模式"
oncommand="snapLinks.init();"
insertafter="context-viewinfo"
class="menuitem-iconic"
image="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAALUlEQVQ4jWNgkEv8TxHGJ/nr18//v379HDWALgbAFJKKqWfAwIfBqAF4JInAAIZN/GgepyNAAAAAAElFTkSuQmCC"
/>
</popup>
</popupset>
<script type="application/x-javascript" xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
var snapLinks = {
timer: null,
button: 0,
init: function() {
this.win = document.commandDispatcher.focusedWindow;
if (this.win == window) this.win = content;
this.doc = this.win.document;
this.body = this.doc.body;
if (!this.body instanceof HTMLBodyElement){
alert("Can not snaplinks.");
return false;
}
this.root = this.doc.documentElement;
this.utils = this.win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
this.popup = document.getElementById("snapLinksMenupopup");
this.bodyCursor = this.body.style.cursor;
this.rootCursor = this.root.style.cursor;
this.body.style.setProperty("cursor", "crosshair", "important");
this.root.style.setProperty("cursor", "crosshair", "important");
this.highlights = [];
this.elements = [];
this.doc.addEventListener("mousedown", this, true);
this.doc.addEventListener("pagehide", this, true);
},
uninit: function() {
this.doc.removeEventListener("mousedown", this, true);
this.doc.removeEventListener("mousemove", this, true);
this.doc.removeEventListener("pagehide", this, true);
removeEventListener("mouseup", this, true);
setTimeout(function(self){
self.doc.removeEventListener("click", self, true);
}, 10, this);
if (this.box && this.box.parentNode)
this.box.parentNode.removeChild(this.box);
this.box = null;
this.body.style.cursor = this.bodyCursor;
this.root.style.cursor = this.rootCursor;
},
destroy: function() {
this.uninit();
this.lowlightAll();
},
handleEvent: function(event) {
switch(event.type){
case "mousedown":
if (event.button != 0 || event.ctrlKey || event.shiftKey || event.altKey) return;
event.preventDefault();
event.stopPropagation();
this.draw(event);
break;
case "mousemove":
event.preventDefault();
event.stopPropagation();
var moveX = event.pageX;
var moveY = event.pageY;
if (this.downX > moveX) this.box.style.left = moveX + "px";
if (this.downY > moveY) this.box.style.top = moveY + "px";
this.box.style.width = Math.abs(moveX - this.downX) + "px";
this.box.style.height = Math.abs(moveY - this.downY) + "px";
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
var timeStamp = new Date().getTime();
if (timeStamp - this.lastHiglightedTime > 150) {
this.boxRect = this.box.getBoundingClientRect();
this.highlightAll();
} else {
var self = this;
this.timer = setTimeout(function() {
self.boxRect = self.box.getBoundingClientRect();
self.highlightAll();
}, 200);
}
break;
case "mouseup":
if (event.button != this.button || event.ctrlKey || event.shiftKey) return;
event.preventDefault();
event.stopPropagation();
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
this.boxRect = this.box.getBoundingClientRect();
this.highlightAll();
for (let [i, e] in Iterator(this.highlights)) {
if (e instanceof HTMLImageElement) {
let link = this.doc.evaluate(
'ancestor::*[@href]', e, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (this.highlights.indexOf(link) === -1) {
this.elements[this.elements.length] = link;
}
continue;
}
this.elements[this.elements.length] = e;
}
this.elements = this.elements;
this.uninit();
this.showPopup(event);
break;
case "click":
event.preventDefault();
event.stopPropagation();
break;
case "pagehide":
this.destroy();
break;
}
},
draw: function(aEvent) {
this.lastHiglightedTime = new Date().getTime();
this.downX = aEvent.pageX;
this.downY = aEvent.pageY;
this.box = this.doc.createElement("div");
this.box.id = "snap-links-box";
this.box.style.cssText = [
'background-color: rgba(0,128,255,.1) !important;'
,'border: 1px solid rgb(255,255,0) !important;'
,'box-sizing: border-box !important;'
,'-moz-box-sizing: border-box !important;'
,'position: absolute !important;'
,'z-index: 2147483647 !important;'
,'top:' + this.downY + 'px;'
,'left:' + this.downX + 'px;'
,'cursor: crosshair !important;'
,'margin: 0px !important;'
,'padding: 0px !important;'
,'outline: none !important;'
].join(" ");
this.body.appendChild(this.box);
this.doc.removeEventListener("mousedown", this, true);
this.doc.addEventListener("mousemove", this, true);
addEventListener("mouseup", this, true);
this.doc.addEventListener("click", this, true);
},
highlightAll: function() {
var a = '[href]:not([href^="javascript:"]):not([href^="mailto:"]):not([href^="#"])';
var selector = a + ', ' + a + ' img, input[type="checkbox"], input[type="radio"]';
var contains = this.getContainsElements();
contains.reverse();
var matches = [];
for (let [i, e] in Iterator(contains)) {
if (e.nodeType !== 1 || !e.mozMatchesSelector(selector))
continue;
if (e.hasAttribute('href')) {
let imgs = Array.slice(e.getElementsByTagName('img'));
if (imgs[0]) {
[].push.apply(contains, imgs);
continue;
}
}
if (!("defStyle" in e))
this.highlight(e);
matches[matches.length] = e;
}
this.highlights.forEach(function(e, i, a){
if (matches.indexOf(e) === -1)
this.lowlight(e);
}, this);
this.highlights = matches;
this.lastHiglightedTime = new Date().getTime();
},
lowlightAll: function() {
this.highlights.forEach(function(e){
this.lowlight(e);
}, this);
},
highlight: function(elem) {
if (!('defStyle' in elem))
elem.defStyle = elem.getAttribute('style');
elem.style.setProperty('outline', '2px solid #ff0000', 'important');
elem.style.setProperty('outline-offset', '-1px', 'important');
},
lowlight: function(elem) {
if ("defStyle" in elem) {
elem.defStyle?
elem.style.cssText = elem.defStyle:
elem.removeAttribute("style");
delete elem.defStyle;
}
},
getContainsElements: function() {
if (!this.boxRect) return;
var { top, right, bottom, left, width, height } = this.boxRect;
// http://piro.sakura.ne.jp/latest/blosxom/mozilla/xul/2010-07-07_nodesfromrect.htm
// nsIDOMNodeList nodesFromRect(in float aX,
// in float aY,
// in float aTopSize,
// in float aRightSize,
// in float aBottomSize,
// in float aLeftSize,
// in boolean aIgnoreRootScrollFrame,
// in boolean aFlushLayout);
return Array.slice(this.utils.nodesFromRect(left, top, 0, width, height, 0, true, false));
},
showPopup: function(aEvent) {
var cls = [];
var linkcount = 0;
var imagecount = 0;
var checkboxcount = 0;
var radiocount = 0;
for (let [i, elem] in Iterator(this.elements)) {
if (elem instanceof HTMLAnchorElement)
linkcount++;
}
for (let [i, elem] in Iterator(this.elements)) {
if (elem instanceof HTMLAnchorElement && /\.(jpe?g|png|gif|bmp)$/i.test(elem.href))
imagecount++;
}
for (let [i, elem] in Iterator(this.elements)) {
if (elem instanceof HTMLInputElement && elem.type === 'checkbox') {
checkboxcount++;
}
}
for (let [i, elem] in Iterator(this.elements)) {
if (elem instanceof HTMLInputElement && elem.type === 'radio') {
radiocount++;
}
}
if ( linkcount > 0 ) cls.push("hasLink");
if ( imagecount > 0 ) cls.push("hasImageLink");
if ( checkboxcount > 0 ) cls.push("hasCheckbox");
if ( radiocount > 0 ) cls.push("hasRadio");
var setCount = function(id, label){
let currentEntry = document.getElementById(id);
if(currentEntry)
currentEntry.setAttribute("label", label);
};
var data = {
"SnapLinksOpenLinks": "在新标签打开所有链接 (" + linkcount + ")",
"SnapLinksCopyLinks": "复制所有链接URL (" + linkcount + ")",
"SnapLinksCopyLinksReverse": "复制所有链接URL (" + linkcount + ")(反向)",
"SnapLinksOpenImageLinks": "在新标签页打开所有图片链接 (" + imagecount + ")",
"SnapLinksImageLinksOnePage": "在一个标签页显示所有图片链接 (" + imagecount + ")",
"SnapLinksCheckBoxSelect": "复选框 - 选中 (" + checkboxcount + ")",
"SnapLinksCheckBoxCancel": "复选框 - 取消 (" + checkboxcount + ")",
"SnapLinksCheckBoxTaggle": "复选框 - 反选 (" + checkboxcount + ")",
"SnapLinksRadioSelect": "单选框 - 选中 (" + radiocount + ")",
"SnapLinksRadioCancel": "单选框 - 取消 (" + radiocount + ")",
};
for(let id in data){
setCount(id, data[id]);
}
if (cls.length > 0) {
this.popup.className = cls.join(' ');
this.popup.openPopupAtScreen(aEvent.screenX, aEvent.screenY);
} else {
this.lowlightAll();
}
},
openLinks : function(regexp){
var obj = {};
for (let [i, elem] in Iterator(this.elements)) {
if (!elem.href || /^(?:javascript:|mailto:|#)/i.test(elem.href)) continue;
if (!regexp || regexp.test(elem.href))
obj[elem.href] = true;
}
for (let [key, val] in Iterator(obj)) {
gBrowser.addTab(key, { ownerTab: gBrowser.mCurrentTab });
}
},
copyLinks : function(regexp, reverse){
var links = this.elements.filter(function(elem){
return elem instanceof HTMLAnchorElement && (!regexp || regexp.test(elem.href))
}).map(function(e) e.href);
// 筛选出重复的
links = this.unique(links);
if(reverse)
links = links.reverse();
if (links.length){
Components.classes["@mozilla.org/widget/clipboardhelper;1"]
.getService(Components.interfaces.nsIClipboardHelper)
.copyString(links.join('\n'));
}
},
imageOnePage : function(){
var htmlsrc = [
'<style>'
,'img { max-width: 100%; max-height: 100%; }'
,'</style>'].join('');
for (let [i, elem] in Iterator(this.elements)) {
if (elem instanceof HTMLAnchorElement && /\.(jpe?g|png|gif|bmp)$/i.test(elem.href))
htmlsrc += '\n<img src="' + elem.href + '">'
}
gBrowser.addTab("data:text/html;charset=utf-8," +
'<html><head><title>' + this.doc.domain + ' 图象列表</title><body>' +
encodeURIComponent(htmlsrc));
},
checkbox : function(bool){
for (let [i, elem] in Iterator(this.elements)) {
if (elem instanceof HTMLInputElement && elem.type === 'checkbox') {
elem.checked = arguments.length == 0?
!elem.checked :
bool;
}
}
},
radio : function(bool){
for (let [i, elem] in Iterator(this.elements)) {
if (elem instanceof HTMLInputElement && elem.type === 'radio') {
elem.checked = arguments.length == 0?
!elem.checked :
bool;
}
}
},
unique: function(a){
var o = {},
r = [],
t;
for (var i = 0, l = a.length; i < l; i++) {
t = a[i];
if(!o[t]){
o[t] = true;
r.push(t);
}
}
return r;
}
};
(function () {
function optionsShowHide() {
if (gContextMenu) {
var isViewable = true;
var SubMenu = document.getElementById("snaplinksMenuEntry");
if (gContextMenu.onLink || gContextMenu.onImage || gContextMenu.onTextInput || gContextMenu.onMailtoLink || gContextMenu.isContentSelected) {
isViewable = false;
}
if (SubMenu)
SubMenu.hidden = !isViewable;
}
}
var menu = document.getElementById("contentAreaContextMenu");
menu.addEventListener("popupshowing", optionsShowHide, false);
})();
]]></script>
</overlay>