-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.js
165 lines (139 loc) · 4.49 KB
/
bootstrap.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
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
function isNativeUI() {
let appInfo = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo);
return (appInfo.ID == "{aa3c5121-dab2-40e2-81ca-7ea25febc110}");
}
function showToast(aWindow, message) {
aWindow.NativeWindow.toast.show(message, "short");
}
function getSiteURI(window) {
var host = window.BrowserApp.selectedTab.window.location.host;
host = host.replace(/^\s*([-\w]*:\/+)?/, "");
try {
return Services.io.newURI("http://" + host, null, null);
}
catch(e) {
return null;
}
}
// Doorhanger UI objects
const doorhangerID = "cwa-doorhanger";
var button_allow, button_delete, button_session, buttons;
function showDoorhanger(window) {
var siteURI = getSiteURI(window);
if(siteURI == null) {
showToast(window, "Failed to get site URI.");
return false;
}
// initialization has to be done here, since the callbacks need 'window'
button_allow = {
label: "Permanently",
callback: function() {
dump("Add permanently to cookie whitelist: \"" + siteURI + "\"");
Services.perms.add(siteURI, "cookie", Ci.nsICookiePermission.ACCESS_ALLOW);
showToast(window, "Added Site permanently to Cookie Whitelist");
}
};
button_session = {
label: "Only this session",
callback: function() {
dump("Add temporarily to cookie whitelist: \"" + siteURI + "\"");
Services.perms.add(siteURI, "cookie", Ci.nsICookiePermission.ACCESS_SESSION);
showToast(window, "Added Site for this session to Cookie Whitelist");
}
};
button_delete = {
label: "Delete permission",
callback: function() {
dump("Remove from cookie whitelist: \"" + siteURI + "\"");
Services.perms.add(siteURI, "cookie", Ci.nsICookiePermission.ACCESS_DEFAULT);
showToast(window, "Removed Site from Cookie Whitelist");
}
};
buttons = [];
switch (Services.perms.testPermission(siteURI, "cookie")) {
case Ci.nsICookiePermission.ACCESS_SESSION:
buttons.push(button_allow);
buttons.push(button_delete);
break;
case Ci.nsICookiePermission.ACCESS_ALLOW:
buttons.push(button_session);
buttons.push(button_delete);
break;
default:
buttons.push(button_allow);
buttons.push(button_session);
// button_delete here could be useful in case of errors
}
window.NativeWindow.doorhanger.show("Allow Cookies for this site?", doorhangerID, buttons);
}
var gMenuEntry = null;
function loadIntoWindow(window) {
if (!window)
return;
if (isNativeUI()) {
gMenuEntry = window.NativeWindow.menu.add(
"Cookie-Whitelist",
null,
function() {
showDoorhanger(window);
});
}
}
function unloadFromWindow(window) {
if (!window)
return;
if (isNativeUI()) {
window.NativeWindow.menu.remove(gMenuEntry);
}
}
/**
* bootstrap.js API
*/
var windowListener = {
onOpenWindow: function(aWindow) {
// Wait for the window to finish loading
let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
domWindow.addEventListener("load", function() {
domWindow.removeEventListener("load", arguments.callee, false);
loadIntoWindow(domWindow);
}, false);
},
onCloseWindow: function(aWindow) {
},
onWindowTitleChange: function(aWindow, aTitle) {
}
};
function startup(aData, aReason) {
let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
// Load into any existing windows
let windows = wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
loadIntoWindow(domWindow);
}
// Load into any new windows
wm.addListener(windowListener);
}
function shutdown(aData, aReason) {
// When the application is shutting down we normally don't have to clean
// up any UI changes made
if (aReason == APP_SHUTDOWN)
return;
let wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
// Stop listening for new windows
wm.removeListener(windowListener);
// Unload from any existing windows
let windows = wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
unloadFromWindow(domWindow);
}
}
function install(aData, aReason) {
}
function uninstall(aData, aReason) {
}