forked from alice0775/userChrome.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoCopyToClipboard.uc.js
154 lines (141 loc) · 4.67 KB
/
autoCopyToClipboard.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
// ==UserScript==
// @name autoCopyToClipboard.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @include main
// @author Alice0775
// @version 2008/02/19 18:00
// @note Ctrl+Shift+C または about:config の clipboard.autocopy を [false]で無効 true で 有効 トグル
// @note designModeやtextarea等編集可能要素なら何もしない
// ==/UserScript==
var autoCopy = {
// --config--
YOURKEYINSPEED: 100,
YOURCLICKSPEED: 500,
// --config--
PREF: "clipboard.autocopy",
timer: null,
menuitem: null,
init: function() {
var overlay =
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:html="http://www.w3.org/1999/xhtml">
<keyset id="mainKeyset">
<key id="key_toggleAutoCopy"
oncommand="autoCopy.toggle();"
key="C"
modifiers="accel, shift"/>
</keyset>
<menupopup id="menu_ToolsPopup">
<menuitem insertbefore="menu_preferences"
id="autoCopy"
label="Auto Copy toggle"
type="checkbox"
checked="false"
accesskey="A;"
oncommand="autoCopy.toggle();"/>
</menupopup>
</overlay>;
overlay = "data:application/vnd.mozilla.xul+xml;charset=utf-8," + encodeURI(overlay.toXMLString());
window.userChrome_js.loadOverlay(overlay, autoCopy);
},
observe: function(){
window.addEventListener("unload", this, false);
document.getElementById("menu_ToolsPopup").addEventListener("popupshowing", this, false);
gBrowser.mPanelContainer.addEventListener('mouseup', this , true);
gBrowser.mPanelContainer.addEventListener('keyup', this , true);
this.menuitem = document.getElementById("autoCopy");
},
uninit: function() {
window.removeEventListener("unload", this, false);
document.getElementById("menu_ToolsPopup").removeEventListener("popupshowing", this, false);
gBrowser.mPanelContainer.removeEventListener('mouseup', this , true);
gBrowser.mPanelContainer.removeEventListener('keyup', this , true);
},
handleEvent: function(aEvent) {
switch(aEvent.type) {
case 'unload':
this.uninit();
break;
case 'popupshowing':
this.popupshowing();
break;
case 'mouseup':
this.mouseup(aEvent);
break;
case 'keyup':
this.keyup(aEvent);
break;
}
},
popupshowing: function() {
if (this.menuitem)
this.menuitem.setAttribute('checked', gPrefService.getBoolPref(this.PREF));
},
toggle: function() {
gPrefService.setBoolPref(this.PREF, !gPrefService.getBoolPref(this.PREF));
},
mouseup: function(aEvent) {
if (this.timer)
clearTimeout(this.timer);
this.timer = setTimeout(function(aEvent, self) {
self.copyToClipboard(aEvent);
}, this.YOURCLICKSPEED, aEvent, this);
},
keyup: function(aEvent) {
if (this.timer)
clearTimeout(this.timer);
this.timer = setTimeout(function(aEvent, self){
self.copyToClipboard(aEvent);
}, this.YOURKEYINSPEED, aEvent, this);
},
copyToClipboard: function(aEvent) {
//ドキュメントとコンテントタイプ
var doc = aEvent.target.ownerDocument;
if(!doc
|| doc.contentType != 'text/plain'
&& doc.contentType != 'text/html'
&& doc.contentType != 'application/xml'
&& doc.contentType != 'application/xhtml+xml')
return;
// designModeなら何もしない
if (Components.lookupMethod(doc, 'designMode').call(doc) == 'on')
return;
// textarea等編集可能要素なら何もしない
if (this.isParentEditableNode(aEvent.target))
return;
// pref chrck
if (!gPrefService.getBoolPref(this.PREF) )
return;
if (aEvent.type == "mouseup" && aEvent.button == 0) {
goDoCommand('cmd_copy');
return;
}
if (aEvent.type == "keyup" && aEvent.shiftKey) {
switch (aEvent.keyCode) {
case KeyEvent.DOM_VK_END:
case KeyEvent.DOM_VK_HOME:
case KeyEvent.DOM_VK_LEFT:
case KeyEvent.DOM_VK_UP:
case KeyEvent.DOM_VK_RIGHT:
case KeyEvent.DOM_VK_DOWN:
// do
goDoCommand('cmd_copy');
}
}
},
isParentEditableNode: function(node) {
while (node && node.parentNode) {
try {
node.QueryInterface(Ci.nsIDOMNSEditableElement);
return node;
}
catch(e) {
}
if (node.isContentEditable || node.contentEditable == 'true')
return node;
node = node.parentNode;
}
return null;
}
}
autoCopy.init();