[Pkg-mozext-commits] [greasemonkey] 07/43: [WIP] GM_openInTab
David Prévot
taffit at moszumanska.debian.org
Sun Feb 22 21:56:09 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository greasemonkey.
commit b2c0b76c119dc7b4017af87197fcb0e7e9dc32d6
Author: Ventero <ventero at ventero.de>
Date: Sat Sep 27 22:00:43 2014 +0200
[WIP] GM_openInTab
---
content/browser.js | 24 ++++++++++++++++++++++++
content/framescript.js | 9 +++++++++
modules/miscapis.js | 29 ++++-------------------------
modules/sandbox.js | 2 +-
4 files changed, 38 insertions(+), 26 deletions(-)
diff --git a/content/browser.js b/content/browser.js
index ab374f8..48847ac 100644
--- a/content/browser.js
+++ b/content/browser.js
@@ -11,6 +11,11 @@ function GM_BrowserUI() {};
GM_BrowserUI.init = function() {
window.addEventListener("load", GM_BrowserUI.chromeLoad, false);
window.addEventListener("unload", GM_BrowserUI.chromeUnload, false);
+
+ var messageManager = Components.classes["@mozilla.org/globalmessagemanager;1"]
+ .getService(Components.interfaces.nsIMessageListenerManager);
+ messageManager.addMessageListener('greasemonkey:open-in-tab',
+ GM_BrowserUI.openInTab);
};
/**
@@ -52,11 +57,30 @@ GM_BrowserUI.chromeLoad = function(e) {
Components.utils.import('resource://greasemonkey/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 newTab = tabBrowser.loadOneTab(aMessage.data.url, {
+ 'inBackground': aMessage.data.inBackground,
+ 'relatedToCurrent': true
+ });
+
+ // TODO: obviously can't return a window here...
+ return /* tabBrowser.getBrowserForTab(newTab).contentWindow */ null;
+}
+
+/**
* The browser XUL has unloaded. Destroy references/watchers/listeners.
*/
GM_BrowserUI.chromeUnload = function() {
diff --git a/content/framescript.js b/content/framescript.js
index e6df7e9..749657a 100644
--- a/content/framescript.js
+++ b/content/framescript.js
@@ -34,6 +34,15 @@ ScriptRunner.prototype.injectScripts = function(aScripts) {
}
}
+ScriptRunner.prototype.openInTab = function(aUrl, aInBackground) {
+ var response = sendSyncMessage('greasemonkey:open-in-tab', {
+ inBackground: aInBackground,
+ url: aUrl
+ });
+
+ return response ? response[0] : null;
+}
+
ScriptRunner.prototype.registeredMenuCommand = function(aCommand) {
var length = this.menuCommands.push(aCommand);
diff --git a/modules/miscapis.js b/modules/miscapis.js
index e5b99d9..d1e8d54 100644
--- a/modules/miscapis.js
+++ b/modules/miscapis.js
@@ -290,33 +290,12 @@ GM_console.prototype.log = function() {
// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
-function GM_openInTab(safeContentWin, url, aLoadInBackground) {
+function GM_openInTab(aScriptRunner, aUrl, aLoadInBackground) {
if ('undefined' == typeof aLoadInBackground) aLoadInBackground = null;
// Resolve URL relative to the location of the content window.
- var baseUri = Services.io.newURI(safeContentWin.location.href, null, null);
- var uri = Services.io.newURI(url, null, baseUri);
-
- // Get the chrome window currently corresponding to the content window, as
- // this might have changed since the script was injected (e.g. by moving
- // the tab to a different window).
- // TODO: sendAsyncMessage("...", {url}), look for browser containing message.target,
- // add tab to that browser
- var chromeWin = getChromeWinForContentWin(safeContentWin);
- var browser = chromeWin.gBrowser;
- var currentTab = browser.tabs[
- browser.getBrowserIndexForDocument(safeContentWin.top.document)];
- var newTab = browser.loadOneTab(
- uri.spec, {'inBackground': aLoadInBackground});
- var newWin = browser.getBrowserForTab(newTab).contentWindow;
-
- var afterCurrent = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Components.interfaces.nsIPrefService)
- .getBranch("browser.tabs.")
- .getBoolPref("insertRelatedAfterCurrent");
- if (afterCurrent) {
- browser.moveTabTo(newTab, currentTab._tPos + 1);
- }
+ var baseUri = Services.io.newURI(aScriptRunner.window.location.href, null, null);
+ var uri = Services.io.newURI(aUrl, null, baseUri);
- return newWin;
+ return aScriptRunner.openInTab(uri.spec, !!aLoadInBackground);
};
diff --git a/modules/sandbox.js b/modules/sandbox.js
index b81f4ef..6f3b190 100644
--- a/modules/sandbox.js
+++ b/modules/sandbox.js
@@ -111,7 +111,7 @@ function createSandbox(aScript, aScriptRunner) {
}
if (GM_util.inArray(aScript.grants, 'GM_openInTab')) {
sandbox.GM_openInTab = GM_util.hitch(
- null, GM_openInTab, contentWin);
+ null, GM_openInTab, aScriptRunner);
}
if (GM_util.inArray(aScript.grants, 'GM_xmlhttpRequest')) {
sandbox.GM_xmlhttpRequest = GM_util.hitch(
--
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