-
Notifications
You must be signed in to change notification settings - Fork 123
/
MouseGestures2_e10s.uc.js
1722 lines (1557 loc) · 66.3 KB
/
MouseGestures2_e10s.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name Mouse Gestures (with Wheel Gesture and Rocker Gesture)
// @namespace http://www.xuldev.org/
// @description Lightweight customizable mouse gestures.
// @include main
// @charset UTF-8
// @author Gomita, Alice0775 since 2018/09/26
// @compatibility 67
// @version 2019/05/23 03:00 Fix 67.0a1 Bug 1492475 The search service init() method should simply return a Promise
// @version 2019/03/21 01:00 fix Bug 1528695 for 67+
// @version 2019/01/21 01:00 reloadAllTabs to reloadTabs
// @version 2018/12/25 20:00 clear wheel gesture flg when right mouseup/down(wip)
// @version 2018/10/24 01:00 fix, some command
// @version 2018/10/10 22:00 fix, Suppressing mousemove event after wheel gesture
// @version 2018/10/03 11:00 add ucjsMouseGestures_helper.executeInContent, ucjsMouseGestures.executeInChrome
// @version 2018/10/03 08:00 add mime/type, content-dispositon (ucjsMouseGestures._imgTYPE, ucjsMouseGestures._imgDISP)
// @version 2018/10/02 02:00 add auto hide for status info
// @version 2018/09/30 24:00 fix Close Tabs to left right (closeMultipleTabs)
// @version 2018/09/30 22:00 fix surplus scroll if doing Wheel Gestures on 60esr
// @version 2018/09/30 03:00 add dispatchEvent command( dispatch event to content from chrome)
// @version 2018/09/30 01:00 fix getting selected text on CodeMirror editor
// @version 2018/09/30 00:00 fix getting selected text on about:addons page
// @version 2018/09/29 19:00 support zoomIn/Out/Reset for pdf.js
// @version 2018/09/29 19:00 add 'Search for "hogehoge..."' to webSearchPopup
// @version 2018/09/29 02:00 fix unused argument
// @version 2018/09/29 01:00 add commandsPopop
// @version 2018/09/29 01:00 fix "Closed Tabs Popup" does not work if UndoListInTabmenuToo.uc.js is not installed
// @version 2018/09/29 00:00 fix commands list (missing arguments webSearchPopup)
// @version 2018/09/28 23:00 add "Closed Tabs Popup" and "Session History Popup"
// @version 2018/09/28 23:00 fix typo(wip)
// @version 2018/09/28 22:40 fix Close other thabs(wip)
// @version 2018/09/28 19:00 fix typo(wip)
// @version 2018/09/28 18:50 fix gestures command(wip)
// @version 2018/09/28 18:30 change gestures command(wip)
// @version 2018/09/28 06:30 fix regression (wip)
// @version 2018/09/28 06:30 add/modify some gesture (wip)
// @version 2018/09/28 06:00 add library(ucjsMouseGestures_helper.hogehoge) (wip)
// @version 2018/09/27 22:00 add outline for hover links (wip)
// @version 2018/09/27 16:00 fix rocker gesture etc (wip)
// @version 2018/09/26 20:40 fix statusinfo in fx60 (wip)
// @version 2018/09/26 20:40 add find command (wip)
// @version 2018/09/26 20:30 fix page scrolled when Wheel Gesture (wip)
// @version 2018/09/26 19:10 fix author; (wip)
// @version 2018/09/26 19:10 fix missing break; (wip)
// @version 2018/09/26 19:00 fix statusinfo (wip)
// @version 2018/09/26 18:30 e10s (wip)
// @original ver. 1.0.20080201
// @homepage http://www.xuldev.org/misc/ucjs.php
// ==/UserScript==
// @note Linux and Mac are not supported.
var ucjsMouseGestures = {
// == config ==
// options
enableWheelGestures: true, // Wheel Gestures (Scroll wheel with holding right-click)
enableRockerGestures: true, // Rocker Gestures (Left-click with holding right-click and vice versa)
STATUSINFO_TIMEOUT: 2000, // timeout(in millisecond) hide status info
// These are the mouse gesture mappings. Customize this as you like.
// Gesture Sequence, UDRL: right-click then move to up down right left
// Wheel Gestures, W+ : right-click then wheel turn down , W- : left-click then wheel turn up
// Rocker Gestures, L<R : right-click then left-click , L>R : left-click then right-click
// Any Gesture Sequence, *hogehoge : Gesture Sequence following that any faesture
// ucjsMouseGestures._lastX, ucjsMouseGestures._lastY : start coordinates
// ucjsMouseGestures._linkURLs ,ucjsMouseGestures._linkdocURLs : link url hover, ownerDocument url
// ucjsMouseGestures._selLinkURLs ,ucjsMouseGestures._selLinkdocURLs: link url in selected, ownerDocument url
// ucjsMouseGestures._docURL : ownerDocument url
// ucjsMouseGestures._linkURL ,ucjsMouseGestures._linkTXT : ownerDocument url : link url, ownerDocument url
// ucjsMouseGestures._imgSRC _imgTYPE _imgDISP: src mime/type contentdisposition
// ucjsMouseGestures._mediaSRC : media src
// ucjsMouseGestures._selectedTXT : selected text
// ucjsMouseGestures._version : browser major version
commands :
[
['L', '戻る', function(){ document.getElementById("Browser:Back").doCommand(); } ],
['R', '進む', function(){ document.getElementById("Browser:Forward").doCommand(); } ],
['', 'タブの履歴をポップアップ', function(){ ucjsMouseGestures_helper.sessionHistoryPopup(); } ],
['', '履歴の先頭へ戻る', function(){ SessionStore.getSessionHistory(gBrowser.selectedTab, history => {gBrowser.gotoIndex(history.entries.length = 0)}); } ],
['', '履歴の末尾へ進む', function(){ SessionStore.getSessionHistory(gBrowser.selectedTab, history => {gBrowser.gotoIndex(history.entries.length - 1)}); } ],
['RULD', 'ひとつ上の階層へ移動', function(){ ucjsMouseGestures_helper.goUpperLevel(); } ],
['ULDR', '数値を増やして移動', function(){ ucjsMouseGestures_helper.goNumericURL(+1); } ],
['DLUR', '数値を減らして移動', function(){ ucjsMouseGestures_helper.goNumericURL(-1); } ],
['UD', 'リロード', function(){ document.getElementById("Browser:Reload").doCommand(); } ],
['UDU', 'リロード(キャッシュ無視)', function(){ document.getElementById("Browser:ReloadSkipCache").doCommand(); } ],
['', 'すべてタブをリロード', function(){ typeof gBrowser.reloadTabs == "function" ? gBrowser.reloadTabs(gBrowser.visibleTabs) : gBrowser.reloadAllTabs(); } ],
['', '読込中止', function(){ document.getElementById("Browser:Stop").doCommand(); } ],
['', 'テキストリンクを新しいタブに開く', function(){ ucjsMouseGestures_helper.openURLsInSelection(); } ],
['*RDL', '選択範囲のリンクをすべてタブに開く', function(){ ucjsMouseGestures_helper.openSelectedLinksInTabs(); } ],
['*RUL', '通過したリンクをすべてタブに開く', function(){ ucjsMouseGestures_helper.openHoverLinksInTabs(); } ],
['', '選択したリンクを保存', function(){ ucjsMouseGestures_helper.saveHoverLinks(); } ],
['', '通過したリンクを保存', function(){ ucjsMouseGestures_helper.saveHoverLinks(); } ],
['', 'コピー', function(){ ucjsMouseGestures_helper.copyText(ucjsMouseGestures.selectedTXT); } ],
['', '通過したリンクをコピー', function(){ ucjsMouseGestures_helper.copyHoverLinks(); } ],
['', '選択したリンクをコピー', function(){ ucjsMouseGestures_helper.copySelectedLinks(); } ],
['', 'リンクを保存',
function(){
let url = ucjsMouseGestures._linkURL;
saveURL(url, url, null, false, false, null, document);
} ],
['LDR', '画像を保存',
function() {
let that = ucjsMouseGestures;
let url = that._imgSRC;
let aShouldBypassCache = false; // skip cache or not
let aSkipPrompt = false; // use file picker or not
let aReferrer = null;
saveImageURL(url, that._imgTYPE, null, aShouldBypassCache,
aSkipPrompt, aReferrer,
document, that._imgTYPE, that._imgDISP,
PrivateBrowsingUtils.isWindowPrivate(window),
Services.scriptSecurityManager.createNullPrincipal({}));
} ],
['UL', '前のタブ', function(){ gBrowser.tabContainer.advanceSelectedTab(-1, true); } ],
['UR', '次のタブ', function(){ gBrowser.tabContainer.advanceSelectedTab(+1, true); } ],
['', '新しいタブを開く', function(){ document.getElementById("cmd_newNavigatorTab").doCommand(); } ],
['', 'タブをピン留めトグル',
function(){ var tab = gBrowser.selectedTab;
tab.pinned ? gBrowser.unpinTab(tab) : gBrowser.pinTab(tab);
} ],
['', 'タブを複製',
function(){
var orgTab = gBrowser.selectedTab;
var newTab = gBrowser.duplicateTab(orgTab);
gBrowser.moveTabTo(newTab, orgTab._tPos + 1);
} ],
['LD', 'タブを閉じる', function(){ document.getElementById("cmd_close").doCommand(); } ],
['', '左側のタブをすべて閉じる', function(){ ucjsMouseGestures_helper.closeMultipleTabs("left"); } ],
['', '右側のタブをすべて閉じる', function(){ ucjsMouseGestures_helper.closeMultipleTabs("right"); } ],
['', '他のタブをすべて閉じる', function(){ gBrowser.removeAllTabsBut(gBrowser.selectedTab); } ],
['DRU', '閉じたタブを元に戻す', function(){ document.getElementById("History:UndoCloseTab").doCommand(); } ],
['', '閉じたタブのリストをポップアップ', function(){ ucjsMouseGestures_helper.closedTabsPopup(); } ],
['', 'すべてのタブを閉じる', function(){ var browser = getBrowser(); var ctab = browser.addTab("about:newtab"); browser.removeAllTabsBut(ctab); } ],
['', 'ウインドウを閉じる', function(){ document.getElementById("cmd_closeWindow").doCommand(); } ],
['', '最小化', function(){ window.minimize(); } ],
['', '最大化/元のサイズ', function(){ window.windowState == 1 ? window.restore() : window.maximize(); } ],
['LDRU', 'フルスクリーン', function(){ document.getElementById("View:FullScreen").doCommand(); } ],
['RU', '上端へスクロール', function(){ goDoCommand("cmd_scrollTop"); } ],
['RD', '下端へスクロール', function(){ goDoCommand("cmd_scrollBottom"); } ],
['U', '上へスクロール', function(){ goDoCommand("cmd_scrollPageUp"); } ],
['D', '下へスクロール', function(){ goDoCommand("cmd_scrollPageDown"); } ],
['W-', 'ズームイン', function(){ ucjsMouseGestures_helper.zoomIn(); } ],
['W+', 'ズームアウト', function(){ ucjsMouseGestures_helper.zoomOut(); } ],
['L<R', 'ズームリセット', function(){ ucjsMouseGestures_helper.zoomReset(); } ],
['DL', 'ページ内検索バー',
function(){
if (ucjsMouseGestures._version <= "60") {
if (gBrowser.getFindBar()) {
gFindBar.hidden? gFindBar.onFindCommand(): gFindBar.close();
} else {
gLazyFindCommand("onFindCommand");
}
} else {
// 61+
gBrowser.getFindBar().then(findbar => {
findbar.hidden? findbar.onFindCommand(): findbar.close();
});
}
} ],
['', '選択テキストで検索',
function(){
BrowserSearch.loadSearchFromContext(ucjsMouseGestures._selectedTXT,
Services.scriptSecurityManager.createNullPrincipal({}));
} ],
['DRD', '選択テキストで検索(検索エンジンポップアップ)', function(){ ucjsMouseGestures_helper.webSearchPopup(ucjsMouseGestures._selectedTXT || ucjsMouseGestures._linkTXT); } ],
['DR', '選択テキストを検索バーにコピー',
function(){
if (BrowserSearch.searchBar)
BrowserSearch.searchBar.value = ucjsMouseGestures._selectedTXT || ucjsMouseGestures._linkTXT;
} ],
['', '選択テキストを検索バーに追加',
function(){
if (BrowserSearch.searchBar.value){
BrowserSearch.searchBar.value = BrowserSearch.searchBar.value + " " +
ucjsMouseGestures._selectedTXT || ucjsMouseGestures._linkTXT;
}else{
BrowserSearch.searchBar.value = ucjsMouseGestures._selectedTXT ||
ucjsMouseGestures._linkTXT;
}
} ],
['', '検索バー(Web検索ボックス)をクリア', function(){ document.getElementById("searchbar").value = ""; } ],
['', 'CSS切り替え', function(){ var styleDisabled = gPageStyleMenu._getStyleSheetInfo(gBrowser.selectedBrowser).authorStyleDisabled; if (styleDisabled) gPageStyleMenu.switchStyleSheet(""); else gPageStyleMenu.disableStyle(); } ],
['UDUD', 'ジェスチャーコマンドをポップアップ', function(){ ucjsMouseGestures_helper.commandsPopop(); } ],
['', '再起動', function(){ ucjsMouseGestures_helper.restart(); } ],
['', 'ブックマークサイドバー', function(){ SidebarUI.toggle("viewBookmarksSidebar"); } ],
['', '履歴サイドバー', function(){ SidebarUI.toggle("viewHistorySidebar"); } ],
['', '最近の履歴を消去', function(){ setTimeout(function(){ document.getElementById("Tools:Sanitize").doCommand(); }, 0); } ],
['', 'ブラウザーコンソール', function(){ ucjsMouseGestures_helper.openBrowserConsole(); } ],
['', 'アドオンマネージャ', function(){ openTrustedLinkIn("about:addons", "tab", {inBackground: false, relatedToCurrent: true}); } ],
['', 'トラブルシューティング情報', function(){ openTrustedLinkIn("about:support", "tab", {inBackground: false, relatedToCurrent: true}); } ],
['', '設定(オプション)', function(){ openTrustedLinkIn("about:preferences", "tab", {inBackground: false, relatedToCurrent: true}); } ],
['', 'weAutopagerizeのトグル',
function(){
ucjsMouseGestures_helper.dispatchEvent(
{ target: "document", type: "AutoPagerizeToggleRequest" } );
} ],
['', 'weAutopagerizeのトグル 方法2',
function(){
ucjsMouseGestures_helper.executeInContent(function aFrameScript() {
content.document.dispatchEvent(new content.Event("AutoPagerizeToggleRequest"));
});
} ],
['', 'ページ内キャンバスをすべて保存',
function() {
let browserMM = gBrowser.selectedBrowser.messageManager;
browserMM.addMessageListener("getCanvas", function fnc(listener) {
browserMM.removeMessageListener("getCanvas", fnc, true);
let data = listener.data;
let i = data.length;
while(i){
let IMGtitle = ("000"+i).slice(-3);
i--;
saveURL(data[i], IMGtitle + ".png", null, false, true, null, document);
}
});
function contentScript() {
function populate(win) {
let data = [];
for (let j = 0; j < win.frames.length; j++) {
data = data.concat(populate(win.frames[j]));
}
let elems = win.document.getElementsByTagName("canvas");
let i = elems.length;
while(i--){
data.push(elems[i].toDataURL("image/png"));
}
return data
}
let data = populate(content.document.defaultView);
sendAsyncMessage("getCanvas", data);
}
let script = 'data:application/javascript;charset=utf-8,' + encodeURIComponent('(' + contentScript.toString() + ')();');
browserMM.loadFrameScript(script, false);
} ],
['', 'frameスクリプトのテスト用',
function() {
//frameスクリプトを実行
ucjsMouseGestures_helper.executeInContent(function aFrameScript(window) {
// the following are available in frame script
// content // window object
// ucjsMouseGestures._document // content.document
// ucjsMouseGestures._target // element at star mouse gestures
// ucjsMouseGestures._linkURL // link url at star mouse gestures(string)
// ucjsMouseGestures._linkTXT // linktext (string)
// ucjsMouseGestures._imgSRC // image src at star mouse gestures(string)(string)
// ucjsMouseGestures._imgTYPE // mime/type (string)
// ucjsMouseGestures._imgDISP // cpntent-disposition (string)
// ucjsMouseGestures._mediaSRC // media src at star mouse gestures(string)(string)(string)
// ucjsMouseGestures._linkElts // links hoverd (array)
// ucjsMouseGestures._selLinkElts // links selected (array)
// ucjsMouseGestures.executeInChrome: function(func, args) // function oject, array [string, ...]
Services.console.logStringMessage("contentScript window: " + window); //should undefined
Services.console.logStringMessage("contentScript this: " + this);
Services.console.logStringMessage("contentScript content: " + content);
Services.console.logStringMessage("contentScript this === content: " + (this === content));
Services.console.logStringMessage("contentScript _target: " + ucjsMouseGestures._target);
/*
Services.console.logStringMessage("contentScript test: " + ucjsMouseGestures._imgSRC);
Services.console.logStringMessage("contentScript test: " +
ucjsMouseGestures._getLinkTEXT(ucjsMouseGestures._target)) ;
*/
// このframeスクリプトからChromeスクリプトを実行するテスト
ucjsMouseGestures.executeInChrome(
function aChromeScript(url, inBackground) {
gBrowser.loadOneTab(
url, {
relatedToCurrent: true,
inBackground: inBackground,
triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal({})
});
},
["http://www.yahoo.co.jp", false]
);
});
}],
],
// == /config ==
_lastX: 0,
_lastY: 0,
_directionChain: "",
_linkdocURLs: [],
_linkURLs: [],
_selLinkdocURLs: [],
_selLinkURLs: [],
_docURL: "",
_linkURL: "",
_linkTXT: "",
_imgSRC: "",
_mediaSRC: "",
_selectedTXT: "",
_version: "",
_isMac: false, // for Mac
get statusinfo() {
if ("StatusPanel" in window) {
// fx61+
return StatusPanel._labelElement.value;
} else {
return XULBrowserWindow.statusTextField.label;
}
},
set statusinfo(val) {
if ("StatusPanel" in window) {
// fx61+
StatusPanel._label = val;
} else {
XULBrowserWindow.statusTextField.label = val;
}
if(this._statusinfotimer)
clearTimeout(this._statusinfotimer);
this._statusinfotimer = setTimeout(() => {this.hideStatusInfo();}, this.STATUSINFO_TIMEOUT);
this._laststatusinfo = val;
return val;
},
get _isMouseDownR() {
return this.__isMouseDownR;
},
set _isMouseDownR(val) {
this.__isMouseDownR = val;
this._isWheelCanceled = false;
return val;
},
init: function() {
this._version = Services.appinfo.version.split(".")[0];
this._isMac = navigator.platform.indexOf("Mac") == 0;
(gBrowser.mPanelContainer || gBrowser.tabpanels).addEventListener("mousedown", this, false);
(gBrowser.mPanelContainer || gBrowser.tabpanels).addEventListener("mouseup", this, false);
(gBrowser.mPanelContainer || gBrowser.tabpanels).addEventListener("contextmenu", this, true);
if (this.enableWheelGestures)
window.addEventListener('wheel', this, true);
messageManager.addMessageListener("ucjsMouseGestures_linkURL_isWheelCancel", this);
messageManager.addMessageListener("ucjsMouseGestures_linkURL_start", this);
messageManager.addMessageListener("ucjsMouseGestures_linkURLs_stop", this);
messageManager.addMessageListener("ucjsMouseGestures_linkURL_dragstart", this);
messageManager.addMessageListener("ucjsMouseGestures_executeInChrome", this);
window.addEventListener("unload", this, false);
},
uninit: function() {
(gBrowser.mPanelContainer || gBrowser.tabpanels).removeEventListener("mousedown", this, false);
(gBrowser.mPanelContainer || gBrowser.tabpanels).removeEventListener("mousemove", this, false);
(gBrowser.mPanelContainer || gBrowser.tabpanels).removeEventListener("mouseup", this, false);
(gBrowser.mPanelContainer || gBrowser.tabpanels).removeEventListener("contextmenu", this, true);
if (this.enableWheelGestures)
window.removeEventListener('wheel', this, true);
messageManager.removeMessageListener("ucjsMouseGestures_linkURL_isWheelCancel", this);
messageManager.removeMessageListener("ucjsMouseGestures_linkURL_start", this);
messageManager.removeMessageListener("ucjsMouseGestures_linkURLs_stop", this);
messageManager.removeMessageListener("ucjsMouseGestures_linkURL_dragstart", this);
messageManager.removeMessageListener("ucjsMouseGestures_executeInChrome", this);
window.removeEventListener("unload", this, false);
},
_isMouseDownL: false,
__isMouseDownR: false,
_suppressContext: false,
_shouldFireContext: false, // for Linux
_isWheelCanceled: false,
_statusinfotimer :null,
_laststatusinfo : "",
hideStatusInfo: function() {
if(this._statusinfotimer)
clearTimeout(this._statusinfotimer);
this._statusinfotimer = null;
if (this._laststatusinfo == this.statusinfo)
this.statusinfo = "";
},
receiveMessage: function(message) {
Services.console.logStringMessage("message from framescript: " + message.name);
switch(message.name) {
case "ucjsMouseGestures_linkURL_isWheelCancel":
return { _isWheelCanceled: this._isWheelCanceled};
break;
case "ucjsMouseGestures_linkURL_start":
this._docURL = message.data.docURL;
this._docCHARSET = message.data.docCHARSET;
this._linkURL = message.data.linkURL;
this._linkTXT = message.data.linkTXT;
this._imgSRC = message.data.imgSRC;
this._imgTYPE = message.data.imgTYPE;
this._mediaSRC = message.data.mediaSRC;
this._selectedTXT = message.data.selectedTXT;
break;
case "ucjsMouseGestures_linkURLs_stop":
this._linkdocURLs = message.data.linkdocURLs.split(" ");
this._linkURLs = message.data.linkURLs.split(" ");
this._selLinkdocURLs = message.data.selLinkdocURLs.split(" ");
this._selLinkURLs = message.data.selLinkURLs.split(" ");
break;
case "ucjsMouseGestures_linkURL_dragstart":
if (this.enableRockerGestures)
this._isMouseDownL = false;
break;
case "ucjsMouseGestures_executeInChrome":
//try {
browser = message.target;
func = message.data.func;
args = JSON.parse(message.data.args);
functionobj = new Function(
func.match(/\((.*)\)\s*\{/)[1],
func.replace(/^function\s*.*\s*\(.*\)\s*\{/, '').replace(/}$/, '')
);
functionobj.apply(window, args);
//} catch(ex) {
// Services.console.logStringMessage("Error in executeInChrome : " /*+ ex*/);
//}
break;
}
return {};
},
handleEvent: function(event) {
switch (event.type) {
case "mousedown":
if (event.button == 2) {
(gBrowser.mPanelContainer || gBrowser.tabpanels).addEventListener("mousemove", this, false);
this._isMouseDownR = true;
this._suppressContext = false;
this._startGesture(event);
if (this.enableRockerGestures && this._isMouseDownL) {
this._isMouseDownR = false;
this._suppressContext = true;
this._directionChain = "L>R";
this._stopGesture(event);
}
} else if (this.enableRockerGestures && event.button == 0) {
this._isMouseDownL = true;
if (this._isMouseDownR) {
this._isMouseDownL = false;
this._suppressContext = true;
this._directionChain = "L<R";
this._stopGesture(event);
}
}
break;
case "mousemove":
if (this._isMouseDownR) {
this._progressGesture(event);
}
break;
case "mouseup":
gBrowser.selectedBrowser.messageManager.sendAsyncMessage("ucjsMouseGestures_mouseup");
(gBrowser.mPanelContainer || gBrowser.tabpanels).removeEventListener("mousemove", this, false);
if ((this._isMouseDownR && event.button == 2) ||
(this._isMouseDownR && this._isMac && event.button == 0 && event.ctrlKey)) {
this._isMouseDownR = false;
if (this._directionChain)
this._suppressContext = true;
this._stopGesture(event);
if (this._shouldFireContext) {
this._shouldFireContext = false;
this._displayContextMenu(event);
}
} else if (this.enableRockerGestures && event.button == 0 && this._isMouseDownL) {
this._isMouseDownL = false;
}
break;
case "contextmenu":
if (this._suppressContext || this._isMouseDownR) {
this._suppressContext = false;
event.preventDefault();
event.stopPropagation();
if (this._isMouseDownR) {
this._shouldFireContext = true;
}
}
break;
case "wheel":
if (this.enableWheelGestures && this._isMouseDownR) {
//Cancel scrolling
event.preventDefault();
event.stopPropagation();
this._isWheelCanceled = true;
this._suppressContext = true;
this._directionChain = "W" + (event.deltaY > 0 ? "+" : "-");
this._stopGesture(event);
} else {
this._isWheelCanceled = false;
}
break;
}
},
_displayContextMenu: function(event) {
var evt = event.originalTarget.ownerDocument.createEvent("MouseEvents");
evt.initMouseEvent(
"contextmenu", true, true, event.originalTarget.defaultView, 0,
event.screenX, event.screenY, event.clientX, event.clientY,
false, false, false, false, 2, null
);
event.originalTarget.dispatchEvent(evt);
},
_startGesture: function(event) {
this._lastX = event.screenX;
this._lastY = event.screenY;
this._directionChain = "";
this._linkdocURLs = [];
this._linkURLs = [];
this._selLinkdocURLs = [];
this._selLinkURLs = [];
},
_progressGesture: function(event) {
var x = event.screenX;
var y = event.screenY;
var distanceX = Math.abs(x - this._lastX);
var distanceY = Math.abs(y - this._lastY);
// minimal movement where the gesture is recognized
const tolerance = 10;
if (distanceX < tolerance && distanceY < tolerance)
return;
// determine current direction
var direction;
if (distanceX > distanceY)
direction = x < this._lastX ? "L" : "R";
else
direction = y < this._lastY ? "U" : "D";
// compare to last direction
var lastDirection = this._directionChain.charAt(this._directionChain.length - 1);
if (direction != lastDirection) {
this._directionChain += direction;
let commandName = "";
for (let command of this.commands) {
if (command[0].substring(0, 1) == "*") {
let cmd = command[0].substring(1);
if (cmd == this._directionChain.substring(this._directionChain.length - cmd.length)) {
commandName = command[1];
break;
}
}
}
if (!commandName)
for (let command of this.commands) {
if (!!command[0] && command[0] == this._directionChain){
commandName = command[1];
break;
}
}
this.statusinfo = "Gesture: " + this._directionChain + " " + commandName;
}
/*
// ホバーしたリンクのURLを記憶
var linkURL = this._getLinkURL(event.target);
if (linkURL && this._linkURLs.indexOf(linkURL) == -1)
this._linkURLs.push(linkURL);
*/
// save current position
this._lastX = x;
this._lastY = y;
},
/*
_getLinkURL: function(aNode)
{
while (aNode) {
if ((aNode instanceof HTMLAnchorElement || aNode instanceof HTMLAreaElement) && aNode.href)
return aNode.href;
aNode = aNode.parentNode;
}
return null;
},
*/
_stopGesture: function(event) {
window.messageManager.broadcastAsyncMessage("ucjsMouseGestures_mouseup");
gBrowser.selectedBrowser.messageManager.sendAsyncMessage("ucjsMouseGestures_linkURLs_request");
try {
if (this._directionChain)
this._performAction(event);
this.statusinfo = "";
}
catch(ex) {
this.statusinfo = ex;
}
/*
this._directionChain = "";
this._linkURLs = null;
*/
},
_performAction: function(event) {
// Services.console.logStringMessage("====" + this._directionChain);
// Any Gesture Sequence
for (let command of this.commands) {
if (command[0].substring(0, 1) == "*") {
let cmd = command[0].substring(1);
if (cmd == this._directionChain.substring(this._directionChain.length - cmd.length)) {
try {
command[2]();
} catch(ex) {
Services.console.logStringMessage("Error in command (" + this._directionChain + ")" /*+ ex*/);
}
this._directionChain = "";
return;
}
}
}
// These are the mouse gesture mappings.
for (let command of this.commands) {
if (command[0] == this._directionChain) {
try {
command[2]();
} catch(ex) {
Services.console.logStringMessage("Error in command (" + this._directionChain + ")" /*+ ex*/);
}
this._directionChain = "";
return;
}
}
// Unknown Gesture
throw "Unknown Gesture: " + this._directionChain;
this._directionChain = "";
}
};
// エントリポイント
ucjsMouseGestures.init();
let ucjsMouseGestures_framescript = {
init: function() {
let framescript = {
_linkURLs: [],
_linkElts: [],
_target: null,
init: function(isMac, enableWheelGestures) {
this._isMac = isMac;
this.enableWheelGestures = enableWheelGestures;
addMessageListener("ucjsMouseGestures_mouseup", this);
addMessageListener("ucjsMouseGestures_linkURLs_request", this);
addMessageListener("ucjsMouseGestures_dispatchKeyEvent", this);
addMessageListener("ucjsMouseGestures_dispatchEvent", this);
addEventListener("mousedown", this, true);
if (this.enableWheelGestures)
addEventListener('wheel', this, true);
ucjsMouseGestures = this;
},
receiveMessage: function(message) {
// Services.console.logStringMessage("====" + message.name);
switch(message.name) {
case "ucjsMouseGestures_mouseup":
removeEventListener("mousemove", this, false);
this.clearStyle();
break;
case "ucjsMouseGestures_linkURLs_request":
let [_selLinkElts, selLinkURLs, selLinkdocURLs] = this.gatherLinkURLsInSelection();
let json = {
linkdocURLs: this._linkdocURLs.join(" "),
linkURLs: this._linkURLs.join(" "),
selLinkdocURLs: selLinkdocURLs.join(" "),
selLinkURLs: selLinkURLs.join(" ")
};
sendSyncMessage("ucjsMouseGestures_linkURLs_stop",
json
);
ucjsMouseGestures._linkElts = this._linkElts;
ucjsMouseGestures._selLinkElts = _selLinkElts;
this.clearStyle();
break;
case "ucjsMouseGestures_dispatchKeyEvent":
this.dispatchKeyEvent(message.data.targetSelector,
message.data.type,
message.data.bubbles,
message.data.cancelable,
/*message.data.viewArg, */
message.data.ctrlKey,
message.data.shiftKey,
message.data.altKey,
message.data.metaKey,
message.data.keyCode,
message.data.charCode,
);
break;
case "ucjsMouseGestures_dispatchEvent":
this.dispatchEvent(message.data);
}
return {};
},
handleEvent: function(event) {
// Services.console.logStringMessage("====" + event.type);
let imgSRC, imgTYPE, imgDISP, linkURL, linkTXT, mediaSRC, selectedTXT, json;
let _isWheelCanceled;
switch(event.type) {
case "mousedown":
if (event.button == 2) {
addEventListener("mousemove", this, false);
}
addEventListener("dragstart", this, true);
this._linkdocURLs = [];
this._linkURLs = [];
this._linkElts = [];
this._selLinkdocURLs = [];
this._selLinkURLs = [];
[imgSRC, imgTYPE, imgDISP] = this._getImgSRC(event.target);
linkURL = this._getLinkURL(event.target);
linkTXT = this._getLinkTEXT(this.link);
mediaSRC = this._getMediaSRC(event.target);
selectedTXT = this._getSelectedText(event.target);
json = {
docURL: event.target.ownerDocument.location.href,
docCHARSET: event.target.ownerDocument.charset,
linkURL: linkURL,
linkTXT: linkTXT,
imgSRC: imgSRC,
imgTYPE: imgTYPE,
imgDISP: imgDISP,
mediaSRC: mediaSRC,
selectedTXT: selectedTXT
};
sendSyncMessage("ucjsMouseGestures_linkURL_start",
json
);
ucjsMouseGestures._document = content.document;
ucjsMouseGestures._target = event.target;
ucjsMouseGestures._linkURL = linkURL;
ucjsMouseGestures._linkTXT = linkTXT;
ucjsMouseGestures._imgSRC = imgSRC;
ucjsMouseGestures._imgTYPE = imgTYPE;
ucjsMouseGestures._imgDISP = imgDISP;
ucjsMouseGestures._mediaSRC = mediaSRC;
break;
case "mousemove":
// ホバーしたリンクのURLを記憶
linkURL = this._getLinkURL(event.target);
if (linkURL && this._linkURLs.indexOf(linkURL) == -1) {
this._linkdocURLs.push(event.target.ownerDocument.location.href);
this._linkURLs.push(linkURL);
this._linkElts.push(event.target);
event.target.style.outline = "1px dashed darkorange";
}
break;
case "wheel":
_isWheelCanceled = sendSyncMessage(
"ucjsMouseGestures_linkURL_isWheelCancel", {})[0]._isWheelCanceled;
if (_isWheelCanceled) {
//Cancel scrolling
event.preventDefault();
event.stopPropagation();
}
break;
case "dragstart":
sendSyncMessage("ucjsMouseGestures_linkURL_dragstart",{});
removeEventListener("mousemove", this, false);
removeEventListener("dragstart", this, true);
break;
}
},
_getSelectedText: function(target) {
return BrowserUtils.getSelectionDetails(content).fullText;
},
_getLinkURL: function(aNode) {
this.link = null;
while (aNode) {
if ((aNode instanceof content.HTMLAnchorElement || aNode instanceof content.HTMLAreaElement) && aNode.href) {
this.link = aNode;
return aNode.href;
}
try {
aNode = aNode.parentNode;
}catch(e){
return null;
}
}
return null;
},
_getImgSRC: function(aNode) {
let aNode0 = aNode;
while (aNode) {
if (aNode instanceof content.HTMLImageElement && aNode.src) {
let aURL = aNode.src
let aContentType = null;
let aContentDisp = null;
try {
let aDoc = aNode.ownerDocument;
aURL = BrowserUtils.makeURI(aURL, aDoc.characterSet);
var imageCache = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.getImgCacheForDocument(aDoc);
var props =
imageCache.findEntryProperties(aURL, aDoc);
if (props) {
aContentType = props.get("type", Ci.nsISupportsCString).data;
aContentDisp = props.get("content-disposition", Ci.nsISupportsCString).data;
}
} catch (e) {
}
return [aURL.spec, aContentType, aContentDisp];
}
aNode = aNode.parentNode;
}
aNode = aNode0;
while (aNode) {
try {
if (aNode instanceof content.HTMLCanvasElement) {
return [aNode.toDataURL("image/png"), "image/png"];
}
} catch(e) {}
aNode = aNode.parentNode;
}
return [null, null, null];
},
_getMediaSRC: function(aNode) {
while (aNode) {
if (aNode instanceof content.HTMLMediaElement && aNode.src) {
return aNode.src;
}
aNode = aNode.parentNode;
}
return null;
},
_getLinkTEXT: function(aNode) {
if (!aNode)
return "";
let text = this._gatherTextUnder(aNode);
if (!text || !text.match(/\S/)) {
text = this.context.link.getAttribute("title");
if (!text || !text.match(/\S/)) {
text = this.context.link.getAttribute("alt");
if (!text || !text.match(/\S/)) {
text = this._getLinkURL(aNode);
}
}
}
return text;
},
_gatherTextUnder: function(root) {
let text = "";
let node = root.firstChild;
let depth = 1;
while (node && depth > 0) {
// See if this node is text.
if (node.nodeType == node.TEXT_NODE) {
// Add this text to our collection.
text += " " + node.data;
} else if (node instanceof content.HTMLImageElement) {
// If it has an "alt" attribute, add that.
let altText = node.getAttribute( "alt" );
if ( altText && altText != "" ) {
text += " " + altText;
}
}
// Find next node to test.
// First, see if this node has children.
if (node.hasChildNodes()) {
// Go to first child.
node = node.firstChild;
depth++;
} else {
// No children, try next sibling (or parent next sibling).
while (depth > 0 && !node.nextSibling) {
node = node.parentNode;
depth--;
}
if (node.nextSibling) {
node = node.nextSibling;
}
}
}
// Strip leading and tailing whitespace.
text = text.trim();
// Compress remaining whitespace.
text = text.replace(/\s+/g, " ");
return text;
},
clearStyle: function() {
this._linkElts.forEach((aElt) => {
aElt.style.outline = "";
});
},
gatherLinkURLsInSelection: function() {
var win = content;
var sel = win.getSelection();
if (!sel || sel.isCollapsed)
return [[], [], []];
var doc = win.document;
var LinkElts = [];
var linkdocURLs = [];
var linkURLs = [];
for (var i = 0; i < sel.rangeCount; i++) {
var range = sel.getRangeAt(i);
var fragment = range.cloneContents();
var treeWalker = fragment.ownerDocument.createTreeWalker(fragment,
content.NodeFilter.SHOW_ELEMENT, null, true);
while (treeWalker.nextNode()) {
var node = treeWalker.currentNode;
if ((node instanceof content.HTMLAnchorElement ||
node instanceof content.HTMLAreaElement) && node.href) {
try {
LinkElts.push(node);
linkdocURLs.push(fragment.ownerDocument.location.href);
linkURLs.push(node.href);
}
catch(ex) {
}
}
}
}
return [LinkElts, linkURLs, linkdocURLs]
},
// func // function object
// args array [string, string, ...]
executeInChrome: function(func, args) {
let json = {
func : func.toString(),
args : JSON.stringify(args)
}
Services.console.logStringMessage("this " + content);
sendAsyncMessage("ucjsMouseGestures_executeInChrome",
json
);
},
dispatchEvent: function(event) {
let targetSelector = event.target;
if (targetSelector == "document") {
content.document.dispatchEvent(new content.Event(event.type, event));
} else {
content.document.querySelector(targetSelector).
dispatchEvent(new content.Event(event.type, event));
}
},
dispatchKeyEvent: function(targetSelector, type, bubbles, cancelable, /*viewArg, */
ctrlKey, altKey, shiftKey, metaKey,
keyCode, charCode) {
content.document.querySelector(targetSelector).dispatchEvent(new content.KeyboardEvent(
type,
{ bubbles : bubbles, cancelable : cancelable,
ctrlKey : ctrlKey,
shiftKey : shiftKey,
altKey : altKey,
metaKey : metaKey,
keyCode : keyCode, charCode : charCode
})
);