/usr/share/xul-ext/greasemonkey/content/browser.js is in xul-ext-greasemonkey 3.8-1~deb8u1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import('chrome://greasemonkey-modules/content/prefmanager.js');
Components.utils.import('chrome://greasemonkey-modules/content/util.js');
// this file is the JavaScript backing for the UI wrangling which happens in
// browser.xul. It also initializes the Greasemonkey singleton which contains
// all the main injection logic, though that should probably be a proper XPCOM
// service and wouldn't need to be initialized in that case.
function GM_BrowserUI() {};
GM_BrowserUI.init = function() {
window.addEventListener("load", GM_BrowserUI.chromeLoad, false);
window.addEventListener("unload", GM_BrowserUI.chromeUnload, false);
window.messageManager.addMessageListener('greasemonkey:open-in-tab',
GM_BrowserUI.openInTab);
window.messageManager.addMessageListener("greasemonkey:DOMContentLoaded",
function (aMessage) {
var contentType = aMessage.data.contentType;
var href = aMessage.data.href;
GM_BrowserUI.checkDisabledScriptNavigation(contentType, href);
});
};
/**
* The browser XUL has loaded. Find the elements we need and set up our
* listeners and wrapper objects.
*/
GM_BrowserUI.chromeLoad = function(aEvent) {
GM_BrowserUI.bundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService)
.createBundle("chrome://greasemonkey/locale/gm-browser.properties");
// Update visual status when enabled state changes.
GM_prefRoot.watch("enabled", GM_BrowserUI.refreshStatus);
GM_BrowserUI.refreshStatus();
document.getElementById("contentAreaContextMenu")
.addEventListener("popupshowing", GM_BrowserUI.contextMenuShowing, false);
GM_BrowserUI.gmSvc = GM_util.getService();
// Reference this once, so that the getter is called at least once, and the
// initialization routines will run, no matter what.
GM_BrowserUI.gmSvc.config;
// Initialize the chrome side handling of menu commands.
GM_MenuCommander.initialize();
GM_BrowserUI.showToolbarButton();
// Make sure this is imported at least once, so its internal timer starts.
Components.utils.import('chrome://greasemonkey-modules/content/stats.js');
};
/**
* Opens the specified URL in a new tab.
*/
GM_BrowserUI.openTab = function(url) {
gBrowser.selectedTab = gBrowser.addTab(url);
};
/**
* Handles tab opening for a GM_openInTab API call.
*/
GM_BrowserUI.openInTab = function(aMessage) {
var browser = aMessage.target;
var tabBrowser = browser.getTabBrowser();
var scriptTab = tabBrowser.getTabForBrowser(browser);
var scriptTabIsCurrentTab = scriptTab == tabBrowser.mCurrentTab;
// Work around a race condition in Firefox code with E10S disabled.
// See #2107 and #2234
// Todo: Remove timeout when http://bugzil.la/1200334 is resolved.
GM_util.timeout(function () {
var newTab = tabBrowser.addTab(
aMessage.data.url,
{
'ownerTab': scriptTab,
'relatedToCurrent': scriptTabIsCurrentTab,
});
var getBool = Services.prefs.getBoolPref;
var prefBg = (aMessage.data.inBackground === null)
? getBool("browser.tabs.loadInBackground")
: aMessage.data.inBackground;
if (scriptTabIsCurrentTab && !prefBg) tabBrowser.selectedTab = newTab;
var prefRel = (aMessage.data.afterCurrent === null)
? getBool("browser.tabs.insertRelatedAfterCurrent")
: aMessage.data.afterCurrent;
if (prefRel) {
tabBrowser.moveTabTo(newTab, scriptTab._tPos + 1);
} else {
tabBrowser.moveTabTo(newTab, tabBrowser.tabs.length - 1);
}
}, 0);
};
/**
* The browser XUL has unloaded. Destroy references/watchers/listeners.
*/
GM_BrowserUI.chromeUnload = function() {
GM_prefRoot.unwatch("enabled", GM_BrowserUI.refreshStatus);
};
/**
* Called when the content area context menu is showing. We figure out whether
* to show our context items.
*/
GM_BrowserUI.contextMenuShowing = function() {
GM_BrowserUI.getUserScriptUrlUnderPointer(function(aUrl) {
var contextItem = document.getElementById("greasemonkey-view-userscript");
var contextSep = document.getElementById("greasemonkey-install-sep");
contextItem.hidden = contextSep.hidden = !aUrl;
});
};
GM_BrowserUI.getUserScriptUrlUnderPointer = function(callback) {
var culprit = gContextMenu.target || document.popupNode;
if (!culprit) {
callback(null);
return;
}
var mm = gBrowser.selectedBrowser.messageManager;
var messageHandler;
messageHandler = function (aMessage) {
mm.removeMessageListener("greasemonkey:context-menu-end", messageHandler);
var href = aMessage.data.href;
if (href && href.match(/\.user\.js(\?|$)/i)) {
callback(href);
} else {
callback(null);
}
};
mm.addMessageListener("greasemonkey:context-menu-end", messageHandler);
mm.sendAsyncMessage(
"greasemonkey:context-menu-start", {}, {"culprit": culprit});
};
GM_BrowserUI.refreshStatus = function() {
var enabledEl = document.getElementById("gm_toggle_enabled");
var checkedEl = document.getElementById("gm_toggle_checked");
if (GM_util.getEnabled()) {
checkedEl.setAttribute('checked', true);
enabledEl.removeAttribute('disabled');
} else {
checkedEl.setAttribute('checked', false);
enabledEl.setAttribute('disabled', 'yes');
}
};
// Not used directly, kept for GreaseFire. See #1507.
GM_BrowserUI.startInstallScript = function(aUri) {
GM_util.showInstallDialog(aUri.spec, gBrowser);
};
GM_BrowserUI.viewContextItemClicked = function() {
GM_BrowserUI.getUserScriptUrlUnderPointer(function(aUrl) {
if (!aUrl) return;
var scope = {};
Components.utils.import(
"chrome://greasemonkey-modules/content/remoteScript.js", scope);
var rs = new scope.RemoteScript(aUrl);
rs.downloadScript(function (aSuccess) {
if (aSuccess) {
rs.showSource(gBrowser);
} else {
alert(rs.errorMessage);
}
});
});
};
GM_BrowserUI.showToolbarButton = function() {
// See #1652. During transition, this might be set, but not readable yet;
// transition happens in an async callback to get addon version. If existing
// version is "0.0" (the default), this hasn't happened yet, so try later.
if ('0.0' == GM_prefRoot.getValue("version")) {
setTimeout(GM_BrowserUI.showToolbarButton, 50);
return;
}
// Once, enforce that the toolbar button is present. For discoverability.
if (!GM_prefRoot.getValue('haveInsertedToolbarbutton')) {
GM_prefRoot.setValue('haveInsertedToolbarbutton', true);
var navbar = document.getElementById("nav-bar");
var newset = navbar.currentSet + ",greasemonkey-tbb";
navbar.currentSet = newset;
navbar.setAttribute("currentset", newset);
document.persist("nav-bar", "currentset");
}
};
GM_BrowserUI.openOptions = function() {
openDialog('chrome://greasemonkey/content/options.xul', null, 'modal,resizable');
};
GM_BrowserUI.checkDisabledScriptNavigation = function(aContentType, aHref) {
if (GM_util.getEnabled()) return;
if (!aHref.match(/\.user\.js$/)) return;
if (aContentType.match(/^text\/(x|ht)ml/)) return;
var buttons = [{
'label': GM_BrowserUI.bundle.GetStringFromName('disabledWarning.enable'),
'accessKey': GM_BrowserUI.bundle.GetStringFromName('disabledWarning.enable.accessKey'),
'popup': null,
'callback': function() {
GM_util.setEnabled(true);
}
},{
'label': GM_BrowserUI.bundle.GetStringFromName('disabledWarning.enableAndInstall'),
'accessKey': GM_BrowserUI.bundle.GetStringFromName('disabledWarning.enableAndInstall.accessKey'),
'popup': null,
'callback': function() {
GM_util.setEnabled(true);
GM_util.showInstallDialog(aHref, gBrowser);
}
},{
'label': GM_BrowserUI.bundle.GetStringFromName('disabledWarning.install'),
'accessKey': GM_BrowserUI.bundle.GetStringFromName('disabledWarning.install.accessKey'),
'popup': null,
'callback': function() {
GM_util.showInstallDialog(aHref, gBrowser);
}
}];
var notificationBox = gBrowser.getNotificationBox();
var notification = notificationBox.appendNotification(
GM_BrowserUI.bundle.GetStringFromName('greeting.msg'),
"install-userscript",
"chrome://greasemonkey/skin/icon16.png",
notificationBox.PRIORITY_WARNING_MEDIUM,
buttons
);
notification.persistence = -1;
};
GM_BrowserUI.init();
/**
* Handle clicking one of the items in the popup. Left-click toggles the enabled
* state, right-click opens in an editor.
*/
function GM_popupClicked(aEvent) {
var script = aEvent.target.script;
if (!script) return;
if ('command' == aEvent.type) {
// left-click: toggle enabled state
script.enabled =! script.enabled;
} else if ('click' == aEvent.type && aEvent.button == 2) {
// right-click: open in editor
GM_util.openInEditor(script);
}
closeMenus(aEvent.target);
}
/**
* When a menu pops up, fill its contents with the list of scripts.
*/
function GM_showPopup(aEvent) {
// Make sure this event was triggered by opening the actual monkey menu,
// not one of its submenus.
if (aEvent.currentTarget != aEvent.target) return;
var mm = getBrowser().mCurrentBrowser.frameLoader.messageManager;
// See #2276
var aEventTarget = aEvent.target;
var callback = null;
callback = function(message) {
mm.removeMessageListener("greasemonkey:frame-urls", callback);
var urls = message.data.urls;
asyncShowPopup(aEventTarget, urls);
};
mm.addMessageListener("greasemonkey:frame-urls", callback);
mm.sendAsyncMessage("greasemonkey:frame-urls", {});
}
function asyncShowPopup(aEventTarget, urls) {
function uniq(a) {
var seen = {}, list = [], item;
for (var i = 0; i < a.length; i++) {
item = a[i];
if (!seen.hasOwnProperty(item))
seen[item] = list.push(item);
}
return list;
}
function scriptsMatching(urls) {
function testMatchURLs(script) {
function testMatchURL(url) {
return script.matchesURL(url);
}
return urls.some(testMatchURL);
}
return GM_util.getService().config.getMatchingScripts(testMatchURLs);
}
function appendScriptAfter(script, point) {
if (script.needsUninstall) return;
var mi = document.createElement("menuitem");
mi.setAttribute("label", script.localized.name);
mi.script = script;
mi.setAttribute("type", "checkbox");
mi.setAttribute("checked", script.enabled.toString());
point.parentNode.insertBefore(mi, point.nextSibling);
return mi;
}
var popup = aEventTarget;
var scriptsFramedEl = popup.getElementsByClassName("scripts-framed-point")[0];
var scriptsTopEl = popup.getElementsByClassName("scripts-top-point")[0];
var scriptsSepEl = popup.getElementsByClassName("scripts-sep")[0];
var noScriptsEl = popup.getElementsByClassName("no-scripts")[0];
// Remove existing menu items, between separators.
function removeMenuitemsAfter(el) {
while (true) {
var sibling = el.nextSibling;
if (!sibling || 'menuseparator' == sibling.tagName) break;
sibling.parentNode.removeChild(sibling);
}
}
removeMenuitemsAfter(scriptsFramedEl);
removeMenuitemsAfter(scriptsTopEl);
urls = uniq(urls);
var runsOnTop = scriptsMatching( [urls.shift()] ); // first url = top window
var runsFramed = scriptsMatching( urls ); // remainder are all its subframes
// drop all runsFramed scripts already present in runsOnTop
for (var i = 0; i < runsOnTop.length; i++) {
var j = 0, item = runsOnTop[i];
while (j < runsFramed.length) {
if (item === runsFramed[j]) {
runsFramed.splice(j, 1);
} else {
j++;
}
}
}
scriptsSepEl.collapsed = !(runsOnTop.length && runsFramed.length);
noScriptsEl.collapsed = !!(runsOnTop.length || runsFramed.length);
var point;
if (runsFramed.length) {
point = scriptsFramedEl;
runsFramed.forEach(
function(script) { point = appendScriptAfter(script, point); });
}
point = scriptsTopEl;
runsOnTop.forEach(
function(script) { point = appendScriptAfter(script, point); });
// Propagate to commands sub-menu.
GM_MenuCommander.onPopupShowing(aEventTarget);
}
/**
* Clean up the menu after it hides to prevent memory leaks
*/
function GM_hidePopup(aEvent) {
// Only handle the actual monkey menu event.
if (aEvent.currentTarget != aEvent.target) return;
// Propagate to commands sub-menu.
GM_MenuCommander.onPopupHiding();
}
// Short-term workaround for #1406: Tab Mix Plus breaks opening links in
// new tabs because it depends on this function, and incorrectly checks for
// existance of GM_BrowserUI instead of it.
function GM_getEnabled() {
return GM_util.getEnabled();
}
|