-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoscript.js
163 lines (149 loc) · 4.93 KB
/
noscript.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
/**
* Copyright (c) 2008 - 2011 by Eric Van Dewoestine
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*
* Integration plugin for noscript extension
*
* Usage:
* :noscript info Shows noscript info for the current page.
* :noscript popup Opens the noscript popup.
* :noscript toggletemp Toggles scripts temporarily on current page.
* :noscript toggleperm Toggles scripts permanently on current page.
*
* Note: when the noscript popup is open, you can scroll through the items
* using the following standard vimperator key bindings:
* j move down one
* k move up one
* l open the sub-menu
* h close the sub-menu
* g jump to the first item in the menu
* G jump to the last item in the menu
* For implementation simplicity, only single character bindings are supported,
* so you cannot supply counts to any of the above.
*
* Tested against NoScript 1.8.6
*
* @version 0.4
*/
/**
* Class which provides support for noscript commands and hooks into the
* noscript popup to provide vimperator scrolling bindings (j,k,l,h,g,G).
*
* Note: There appears to be a bug in firefox's menupopup key bindings, where
* if you navigate to a sub popup (l or right arrow), and navigate back to the
* main popup (h or left arrow), then the menupopup seems to lose the ability
* to navigate back to the sub menu, and loses the ability to close the main
* popup via esc (an alt-tab should still close it). This behavior is
* reproducable with or without this plugin.
*/
function NoscriptVimperator() {
var popup = (
noscriptOverlay.stickyUI &&
noscriptOverlay.ns.getPref("stickyUI.onKeyboard") &&
(popup = noscriptOverlay.stickyUI)
) || document.getElementById("noscript-status-popup");
popup.addEventListener('popupshown', popupshown, true);
popup.addEventListener('popuphidden', popuphidden, true);
function popupshown(event){
if (event.target == popup){
window.addEventListener("keypress", keypress, true);
}
}
function popuphidden(event){
if (event.target == popup){
window.removeEventListener("keypress", keypress, true);
}
}
function keypress(event){
var keyCode = null;
switch(String.fromCharCode(event.which)){
case "j":
keyCode = 40;
break;
case "k":
keyCode = 38;
break;
case "l":
keyCode = 39;
break;
case "h":
keyCode = 37;
break;
case "G":
keyCode = 35;
break;
case "g":
keyCode = 36;
break;
default:
break;
}
if (keyCode){
var newEvent = new KeyboardEvent("keydown", {keyCode: keyCode});
popup.dispatchEvent(newEvent);
}
}
return {
info: function(){
liberator.echo(util.objectToString(noscriptOverlay.getSites(), true));
},
popup: function(){
noscriptOverlay.showUI();
},
toggletemp: function(){
noscriptOverlay.toggleCurrentPage(3);
},
togglerevokeall: function(){
noscriptOverlay.revokeTemp();
},
toggleperm: function(){
const ns = noscriptOverlay.ns;
const url = ns.getQuickSite(content.document.documentURI, /*level*/ 3);
noscriptOverlay.safeAllow(url, !ns.isJSEnabled(url), false);
},
_execute: function(args){
var name = args.shift();
var cmd = nsv[name];
if (!cmd){
liberator.echoerr('Unsupported noscript command: ' + name);
return false;
}
return cmd(args);
},
_completer: function(context){
var commands = [];
for (var name in nsv){
if (name.indexOf('_') !== 0 && nsv.hasOwnProperty(name)){
commands.push([name,""]);
}
}
return [0,commands];
}
};
}
if (typeof(noscriptOverlay) != 'undefined'){
var nsv = NoscriptVimperator();
commands.addUserCommand(["nosc[ript]"],
"Execute noscript commands",
function(args) { nsv._execute(args); },
{ completer: nsv._completer }
);
}