-
Notifications
You must be signed in to change notification settings - Fork 37
/
allowTab.uc.js
100 lines (93 loc) · 3.3 KB
/
allowTab.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
// ==UserScript==
// @name allowTab.uc.js
// @description タブごとに画像や JS の有効/無効を切り替える
// @namespace http://d.hatena.ne.jp/Griever/
// @author Griever
// @include main
// @compatibility Firefox 4
// @charset UTF-8
// @version 0.0.3
// @note 0.0.3 e10s に対応
// ==/UserScript==
"use strict";
var allowTab = {
list : ["allowImages", "allowJavascript", "allowMetaRedirects", "allowPlugins", "allowSubframes"],
init: function(){
this.popup = document.getElementById('tabContextMenu') ||
document.getAnonymousElementByAttribute(gBrowser, "anonid", "tabContextMenu");
this.separator = this.popup.appendChild(document.createElement('menuseparator'));
this.menuitems = this.list.map(function(type){
var menuitem = document.createElement('menuitem');
menuitem.setAttribute("type", "checkbox");
//menuitem.setAttribute("autocheck", "false");
menuitem.setAttribute("checked", "true");
menuitem.setAttribute("label", type);
menuitem.setAttribute("id", type);
//menuitem.setAttribute("closemenu", "none");
menuitem.setAttribute("onclick", "allowTab.onClick(event);");
menuitem.setAttribute("oncommand", "allowTab.onCommand(event);");
return this.popup.appendChild(menuitem);
}, this);
this.popup.addEventListener('popupshowing', this, false);
window.addEventListener('unload', this, false);
},
uninit: function(){
this.popup.removeEventListener('popupshowing', this, false);
window.removeEventListener('unload', this, false);
},
destroy: function() {
this.uninit();
this.separator.parentNode.removeChild(this.separator);
this.menuitems.forEach(function(e){ e.parentNode.removeChild(e); });
},
handleEvent : function(event){
switch(event.type){
case 'popupshowing':
if (event.target != event.currentTarget) return;
var tab = document.popupNode || this.popup.triggerNode;
if (tab.localName != 'tab')
tab = gBrowser.mCurrentTab;
var bmm = tab.linkedBrowser.messageManager;
var listener = (message) => {
bmm.removeMessageListener('allowTab:ResponseState', listener);
for (let m of this.menuitems) {
m.setAttribute('checked', message.data[m.id]);
}
};
bmm.addMessageListener('allowTab:ResponseState', listener);
bmm.loadFrameScript('data:text/javascript;charset=utf-8,' + encodeURIComponent(
`
sendAsyncMessage('allowTab:ResponseState', {
allowImages: this.docShell.allowImages,
allowJavascript: this.docShell.allowJavascript,
allowMetaRedirects: this.docShell.allowMetaRedirects,
allowPlugins: this.docShell.allowPlugins,
allowSubframes: this.docShell.allowSubframes
});
`
), true);
break;
case 'unload':
this.uninit();
break;
}
},
onCommand: function(event) {
var command = event.currentTarget.id;
var tab = document.popupNode || this.popup.triggerNode;
if (tab.localName != 'tab')
tab = gBrowser.mCurrentTab;
var bmm = tab.linkedBrowser.messageManager;
bmm.loadFrameScript('data:text/javascript;charset=utf-8,' + encodeURIComponent(
`
this.docShell.${ command } = !this.docShell.${ command }
`
), true);
},
onClick: function(event) {
if (event.button != 1) return;
allowTab.onCommand(event);
event.target.setAttribute("checked", event.target.getAttribute("checked") != "true");
},
}
allowTab.init();