[Pkg-mozext-commits] [greasemonkey] 10/45: Refactor parts of the menu command handling into a JSM.
David Prévot
taffit at moszumanska.debian.org
Mon Nov 3 20:59:19 UTC 2014
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository greasemonkey.
commit 104a3e37fb7865de7e4c8722814c91d3d3fcb02f
Author: Ventero <ventero at ventero.de>
Date: Sun Sep 21 00:35:37 2014 +0200
Refactor parts of the menu command handling into a JSM.
---
components/greasemonkey.js | 68 ++++++----------------------------------------
content/menucommander.js | 6 +++-
modules/menucommand.js | 62 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 75 insertions(+), 61 deletions(-)
diff --git a/components/greasemonkey.js b/components/greasemonkey.js
index 57f994d..402a8f8 100644
--- a/components/greasemonkey.js
+++ b/components/greasemonkey.js
@@ -11,6 +11,7 @@ var Cu = Components.utils;
Cu.import("resource://greasemonkey/third-party/getChromeWinForContentWin.js");
Cu.import('resource://greasemonkey/GM_setClipboard.js');
Cu.import('resource://greasemonkey/constants.js');
+Cu.import("resource://greasemonkey/menucommand.js");
Cu.import("resource://greasemonkey/miscapis.js");
Cu.import("resource://greasemonkey/parseScript.js");
Cu.import("resource://greasemonkey/prefmanager.js");
@@ -22,7 +23,6 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
// Only a particular set of strings are allowed. See: http://goo.gl/ex2LJ
var gMaxJSVersion = "ECMAv5";
-var gMenuCommands = [];
var gStartupHasRun = false;
var gScriptEndingRegexp = new RegExp('\\.user\\.js$');
@@ -163,40 +163,6 @@ function isTempScript(uri) {
return gTmpDir.contains(file, true);
}
-function registerMenuCommand(
- wrappedContentWin, script,
- commandName, commandFunc, accessKey, unused, accessKey2
-) {
- if (wrappedContentWin.top != wrappedContentWin) {
- // Only register menu commands for the top level window.
- return;
- }
-
- // Legacy support: if all five parameters were specified, (from when two
- // were for accelerators) use the last one as the access key.
- if ('undefined' != typeof accessKey2) {
- accessKey = accessKey2;
- }
-
- if (accessKey
- && (("string" != typeof accessKey) || (accessKey.length != 1))
- ) {
- throw new Error(
- gStringBundle.GetStringFromName('error.menu-invalid-accesskey')
- .replace('%1', commandName)
- );
- }
-
- var command = {
- name: commandName,
- accessKey: accessKey,
- commandFunc: commandFunc,
- contentWindow: wrappedContentWin,
- contentWindowId: GM_util.windowId(wrappedContentWin),
- frozen: false};
- gMenuCommands.push(command);
-};
-
function runScriptInSandbox(script, sandbox) {
// Eval the code, with anonymous wrappers when/if appropriate.
function evalWithWrapper(code, fileName) {
@@ -370,26 +336,23 @@ service.prototype.__defineGetter__('config', function() {
});
service.prototype.contentDestroyed = function(aContentWindowId) {
- this.withAllMenuCommandsForWindowId(null, function(index, command) {
- if (GM_util.windowIsClosed(command.contentWindow)
- // This content destroyed message matches the command's window id.
- || (aContentWindowId && (command.contentWindowId == aContentWindowId))
- ) {
- // If the window is closed, remove the reference to it.
- gMenuCommands.splice(index, 1);
- }
+ removeMatchingMenuCommands(null, function(index, command) {
+ // Remove the reference if either the window is closed, ...
+ return (GM_util.windowIsClosed(command.contentWindow)
+ // ... or the content destroyed message matches the command's window id.
+ || (aContentWindowId && (command.contentWindowId == aContentWindowId)));
}, true); // Don't forget the aForced=true passed here!
};
service.prototype.contentFrozen = function(contentWindowId) {
if (!contentWindowId) return;
- this.withAllMenuCommandsForWindowId(contentWindowId,
+ withAllMenuCommandsForWindowId(contentWindowId,
function(index, command) { command.frozen = true; });
};
service.prototype.contentThawed = function(contentWindowId) {
if (!contentWindowId) return;
- this.withAllMenuCommandsForWindowId(contentWindowId,
+ withAllMenuCommandsForWindowId(contentWindowId,
function(index, command) { command.frozen = false; });
};
@@ -447,21 +410,6 @@ service.prototype.injectScripts = function(
}
};
-service.prototype.withAllMenuCommandsForWindowId = function(
- aContentWindowId, aCallback, aForce
-) {
- if(!aContentWindowId && !aForce) return;
-
- var l = gMenuCommands.length - 1;
- for (var i = l, command = null; command = gMenuCommands[i]; i--) {
- if (aForce
- || (command.contentWindowId == aContentWindowId)
- ) {
- aCallback(i, command);
- }
- }
-};
-
//////////////////////////// Component Registration ////////////////////////////
var NSGetFactory = XPCOMUtils.generateNSGetFactory([service]);
diff --git a/content/menucommander.js b/content/menucommander.js
index 976030f..b2da3f2 100644
--- a/content/menucommander.js
+++ b/content/menucommander.js
@@ -27,7 +27,11 @@ GM_MenuCommander.onPopupShowing = function(aMenuPopup) {
var windowId = GM_util.windowId(gBrowser.contentWindow);
if (windowId) {
- GM_BrowserUI.gmSvc.withAllMenuCommandsForWindowId(
+ // Avoid polluting the global namespace with the exports of this module.
+ var scope = {};
+ Components.utils.import('resource://greasemonkey/menucommand.js', scope);
+
+ scope.withAllMenuCommandsForWindowId(
windowId,
function(index, command) {
if (command.frozen) return;
diff --git a/modules/menucommand.js b/modules/menucommand.js
new file mode 100644
index 0000000..aea292d
--- /dev/null
+++ b/modules/menucommand.js
@@ -0,0 +1,62 @@
+var EXPORTED_SYMBOLS = ['registerMenuCommand', 'removeMatchingMenuCommands',
+ 'withAllMenuCommandsForWindowId'];
+
+Components.utils.import("resource://gre/modules/Services.jsm");
+Components.utils.import('resource://greasemonkey/util.js');
+
+var gMenuCommands = [];
+var gStringBundle = Services.strings.createBundle(
+ "chrome://greasemonkey/locale/greasemonkey.properties");
+
+function registerMenuCommand(
+ wrappedContentWin, script,
+ commandName, commandFunc, accessKey, unused, accessKey2
+) {
+ if (wrappedContentWin.top != wrappedContentWin) {
+ // Only register menu commands for the top level window.
+ return;
+ }
+
+ // Legacy support: if all five parameters were specified, (from when two
+ // were for accelerators) use the last one as the access key.
+ if ('undefined' != typeof accessKey2) {
+ accessKey = accessKey2;
+ }
+
+ if (accessKey
+ && (("string" != typeof accessKey) || (accessKey.length != 1))
+ ) {
+ throw new Error(
+ gStringBundle.GetStringFromName('error.menu-invalid-accesskey')
+ .replace('%1', commandName)
+ );
+ }
+
+ var command = {
+ name: commandName,
+ accessKey: accessKey,
+ commandFunc: commandFunc,
+ contentWindow: wrappedContentWin,
+ contentWindowId: GM_util.windowId(wrappedContentWin),
+ frozen: false};
+ gMenuCommands.push(command);
+};
+
+function withAllMenuCommandsForWindowId(aContentWindowId, aCallback, aForce) {
+ if(!aContentWindowId && !aForce) return;
+
+ var l = gMenuCommands.length - 1;
+ for (var i = l, command = null; command = gMenuCommands[i]; i--) {
+ if (aForce || command.contentWindowId == aContentWindowId) {
+ aCallback(i, command);
+ }
+ }
+};
+
+function removeMatchingMenuCommands(aContentWindowId, aCallback, aForce) {
+ withAllMenuCommandsForWindowId(aContentWindowId, function(index, command) {
+ if (aCallback(index, command)) {
+ gMenuCommands.splice(index, 1);
+ }
+ }, aForce)
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/greasemonkey.git
More information about the Pkg-mozext-commits
mailing list