Permalink
Browse files

'Install' button in notification bar works again.

  • Loading branch information...
1 parent aa33cd2 commit f8363f52f9f4bb884a3588e4d0248e3d1c556502 @arantius arantius committed Dec 1, 2011
Showing with 123 additions and 128 deletions.
  1. +9 −47 components/greasemonkey.js
  2. +10 −68 content/browser.js
  3. +3 −9 content/install.js
  4. +45 −4 modules/remoteScript.js
  5. +56 −0 modules/util/showInstallDialog.js
View
@@ -31,8 +31,12 @@ var gMaxJSVersion = "1.8";
var gMenuCommands = [];
var gStartupHasRun = false;
-var gWindowWatcher = Cc["@mozilla.org/embedcomp/window-watcher;1"]
- .getService(Ci.nsIWindowWatcher);
+var gFileProtocolHandler = Components
+ .classes["@mozilla.org/network/protocol;1?name=file"]
+ .getService(Ci.nsIFileProtocolHandler);
+var gTmpDir = Components.classes["@mozilla.org/file/directory_service;1"]
+ .getService(Components.interfaces.nsIProperties)
+ .get("TmpD", Components.interfaces.nsILocalFile);
/////////////////////// Component-global Helper Functions //////////////////////
@@ -149,52 +153,10 @@ function getFirebugConsole(wrappedContentWin, chromeWin) {
}
}
-function installDialog(aUri, aBrowser, aService) {
- var scope = {};
- Cu.import('resource://greasemonkey/remoteScript.js', scope);
- var rs = new scope.RemoteScript(aUri.spec);
-
- rs.onScriptMeta(function(aRemoteScript, aType, aScript) {
- var params = [rs, aScript];
- params.wrappedJSObject = params;
- // TODO: Find a better fix than this sloppy workaround.
- // Apparently this version of .openWindow() blocks; and called by the
- // "script meta data available" callback as this is, blocks the further
- // download of the script!
- var curriedOpenWindow = GM_util.hitch(
- null, gWindowWatcher.openWindow,
- /* aParent */ null,
- 'chrome://greasemonkey/content/install.xul',
- /* aName */ null,
- 'chrome,centerscreen,modal,dialog,titlebar,resizable',
- params);
- GM_util.timeout(0, curriedOpenWindow);
- });
-
- rs.download(function(aSuccess, aType) {
- if (!aSuccess) {
- if ('script' == aType) {
- // Failure downloading script; browse to it.
- aService.ignoreNextScript();
- // TODO: Test this in Firefox 3.
- aBrowser.loadURI(aUri.spec, /* aReferrer */ null, /* aCharset */ null);
- }
- }
- });
-}
-
function isTempScript(uri) {
if (uri.scheme != "file") return false;
-
- var fph = Components.classes["@mozilla.org/network/protocol;1?name=file"]
- .getService(Ci.nsIFileProtocolHandler);
-
- var file = fph.getFileFromURLSpec(uri.spec);
- var tmpDir = Components.classes["@mozilla.org/file/directory_service;1"]
- .getService(Components.interfaces.nsIProperties)
- .get("TmpD", Components.interfaces.nsILocalFile);
-
- return file.parent.equals(tmpDir) && file.leafName != "newscript.user.js";
+ var file = gFileProtocolHandler.getFileFromURLSpec(uri.spec);
+ return gTmpDir.contains(file, true);
}
function openInTab(safeContentWin, chromeWin, url, aLoadInBackground) {
@@ -375,7 +337,7 @@ service.prototype.shouldLoad = function(ct, cl, org, ctx, mt, ext) {
&& cl.spec.match(/\.user\.js$/)
) {
if (!this._ignoreNextScript && !isTempScript(cl)) {
- installDialog(cl, ctx, this);
+ GM_util.showInstallDialog(cl.spec, ctx, this);
ret = Ci.nsIContentPolicy.REJECT_REQUEST;
}
View
@@ -107,16 +107,6 @@ GM_BrowserUI.contentLoad = function(event) {
if (href.replace(/#.*/, '') == event.target.documentURI.replace(/#.*/, '')) {
GM_BrowserUI.gmSvc.runScripts('document-end', safeWin, window);
}
-
- // Show the greasemonkey install banner if we are navigating to a .user.js
- // file in a top-level tab. If the file was previously cached it might have
- // been given a number after .user, like gmScript.user-12.js
- if (safeWin == safeWin.top &&
- href.match(/\.user(?:-\d+)?\.js$/) &&
- !/text\/html/i.test(safeWin.document.contentType)) {
- var browser = GM_BrowserUI.tabBrowser.getBrowserForDocument(safeWin.document);
- GM_BrowserUI.showInstallBanner(browser);
- }
};
GM_BrowserUI.pagehide = function(aEvent) {
@@ -134,50 +124,6 @@ GM_BrowserUI.pageshow = function(aEvent) {
};
/**
- * Shows the install banner across the top of the tab that is displayed when
- * a user selects "show script source" in the install dialog.
- */
-GM_BrowserUI.showInstallBanner = function(browser) {
- var greeting = GM_BrowserUI.bundle.getString("greeting.msg");
-
- var notificationBox = GM_BrowserUI.tabBrowser.getNotificationBox(browser);
-
- // Remove existing notifications. Notifications get removed
- // automatically onclick and on page navigation, but we need to remove
- // them ourselves in the case of reload, or they stack up.
- for (var i = 0, child; child = notificationBox.childNodes[i]; i++) {
- if (child.getAttribute("value") == "install-userscript") {
- notificationBox.removeNotification(child);
- }
- }
-
- notificationBox.appendNotification(
- greeting,
- "install-userscript",
- "chrome://greasemonkey/skin/icon16.png",
- notificationBox.PRIORITY_WARNING_MEDIUM,
- [{
- label: GM_BrowserUI.bundle.getString("greeting.btn"),
- accessKey: GM_BrowserUI.bundle.getString("greeting.btnAccess"),
- popup: null,
- callback: GM_BrowserUI.installCurrentScript
- }]
- );
-};
-
-
-/**
- * Open the tab to show the contents of a script and display the banner to let
- * the user install it.
- */
-GM_BrowserUI.showScriptView = function(scriptDownloader) {
- GM_BrowserUI._scriptDownloader = scriptDownloader;
-
- var tab = GM_BrowserUI.tabBrowser.addTab(scriptDownloader.script.previewURL);
- GM_BrowserUI.tabBrowser.selectedTab = tab;
-};
-
-/**
* Implements nsIObserve.observe. Right now we're only observing our own
* install-userscript, which happens when the install bar is clicked.
*/
@@ -201,13 +147,6 @@ GM_BrowserUI.openTab = function(url) {
}
/**
- * Handles the install button getting clicked.
- */
-GM_BrowserUI.installCurrentScript = function() {
- GM_BrowserUI._scriptDownloader.installScript();
-};
-
-/**
* The browser XUL has unloaded. Destroy references/watchers/listeners.
*/
GM_BrowserUI.chromeUnload = function() {
@@ -270,13 +209,16 @@ GM_BrowserUI.viewContextItemClicked = function() {
var uri = GM_BrowserUI.getUserScriptLinkUnderPointer();
if (!uri) return;
- var doc = document.popupNode.ownerDocument;
- GM_util.getService().ignoreNextScript();
- // TODO: Is this right for Firefox 3?
- openLinkIn(uri.spec, 'tab', {
- 'charset': doc.characterset,
- 'referrerURI': doc.documentURIObject
- });
+ var scope = {};
+ Components.utils.import('resource://greasemonkey/remoteScript.js', scope);
+ var rs = new scope.RemoteScript(uri.spec);
+ rs.downloadScript(function(aSuccess) {
+ if (aSuccess) {
+ rs.showSource(gBrowser);
+ } else {
+ alert(rs.errorMessage);
+ }
+ });
};
GM_BrowserUI.showToolbarButton = function() {
View
@@ -1,7 +1,8 @@
Components.utils.import('resource://greasemonkey/util.js');
var gRemoteScript = window.arguments[0].wrappedJSObject[0];
-var gScript = window.arguments[0].wrappedJSObject[1];
+var gBrowser = window.arguments[0].wrappedJSObject[1];
+var gScript = window.arguments[0].wrappedJSObject[2];
var gHtmlNs = 'http://www.w3.org/1999/xhtml';
var gAcceptButton = null;
@@ -97,14 +98,7 @@ function onProgress(aRemoteScript, aEventType, aData) {
}
function onShowSource() {
- gRemoteScript.cleanup();
- GM_util.getService().ignoreNextScript();
-
- var win = GM_util.getBrowserWindow();
- // TODO: Test this in Firefox 3.
- win.gBrowser.loadURI(
- gRemoteScript.url, /* aReferrer */ null, /* aCharset */ null);
-
+ gRemoteScript.showSource(gBrowser);
window.setTimeout(window.close, 0);
}
View
@@ -21,6 +21,10 @@ var stringBundle = Components
.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService)
.createBundle("chrome://greasemonkey/locale/greasemonkey.properties");
+var stringBundleBrowser = Components
+ .classes["@mozilla.org/intl/stringbundle;1"]
+ .getService(Components.interfaces.nsIStringBundleService)
+ .createBundle("chrome://greasemonkey/locale/gm-browser.properties");
/////////////////////////////// Private Helpers ////////////////////////////////
@@ -177,10 +181,16 @@ RemoteScript.prototype.download = function(aCompletionCallback) {
assertIsFunction(
aCompletionCallback, 'Completion callback is not a function.');
- this.downloadScript(GM_util.hitch(this, function(aSuccess, aPoint) {
- if (aSuccess) this._downloadDependencies(aCompletionCallback);
- aCompletionCallback(aSuccess, aPoint);
- }));
+ if (this.script) {
+ // TODO: Verify that this condition really is sufficient. Is the script
+ // completely loaded?
+ this._downloadDependencies(aCompletionCallback);
+ } else {
+ this.downloadScript(GM_util.hitch(this, function(aSuccess, aPoint) {
+ if (aSuccess) this._downloadDependencies(aCompletionCallback);
+ aCompletionCallback(aSuccess, aPoint);
+ }));
+ }
};
/** Download just enough of the script to find the metadata. */
@@ -238,6 +248,37 @@ RemoteScript.prototype.onScriptMeta = function(aCallback) {
this._scriptMetaCallbacks.push(aCallback);
};
+RemoteScript.prototype.showSource = function(aTabBrowser) {
+ // Turn standard browser into tab browser, if necessary.
+ if (aTabBrowser.getTabBrowser) aTabBrowser = aTabBrowser.getTabBrowser()
+
+ if (this._progress[0] < 1) {
+ throw new Error('Script is not loaded!');
+ }
+
+ var tab = aTabBrowser.loadOneTab(
+ ioService.newFileURI(this._scriptFile).spec,
+ {'inBackground': false});
+ var notificationBox = aTabBrowser.getNotificationBox();
+ notificationBox.appendNotification(
+ stringBundleBrowser.GetStringFromName('greeting.msg'),
+ "install-userscript",
+ "chrome://greasemonkey/skin/icon16.png",
+ notificationBox.PRIORITY_WARNING_MEDIUM,
+ [{
+ 'label': stringBundleBrowser.GetStringFromName('greeting.btn'),
+ 'accessKey': stringBundleBrowser.GetStringFromName('greeting.btnAccess'),
+ 'popup': null,
+ 'callback': GM_util.hitch(this, function() {
+ GM_util.showInstallDialog(this, aTabBrowser, GM_util.getService());
+ // Timeout puts this after the notification closes itself for the
+ // button click, avoiding an error inside that (Firefox) code.
+ GM_util.timeout(0, function() { aTabBrowser.removeTab(tab); });
+ })
+ }]
+ );
+};
+
RemoteScript.prototype.toString = function() {
return '[RemoteScript object; ' + this._url + ']';
};
@@ -0,0 +1,56 @@
+Components.utils.import('resource://greasemonkey/util.js');
+
+var EXPORTED_SYMBOLS = ['showInstallDialog'];
+
+Components.utils.import('resource://greasemonkey/remoteScript.js');
+var gWindowWatcher = Components
+ .classes["@mozilla.org/embedcomp/window-watcher;1"]
+ .getService(Components.interfaces.nsIWindowWatcher);
+
+function showInstallDialog(aUrlOrRemoteScript, aBrowser, aService) {
+ var rs = null;
+ if ('string' == typeof aUrlOrRemoteScript) {
+ rs = new RemoteScript(aUrlOrRemoteScript);
+ } else {
+ rs = aUrlOrRemoteScript;
+ }
+
+ var params = null;
+ function openDialog() {
+ // TODO: Find a better fix than this sloppy workaround.
+ // Apparently this version of .openWindow() blocks; and called by the
+ // "script meta data available" callback as this is, blocks the further
+ // download of the script!
+ var curriedOpenWindow = GM_util.hitch(
+ null, gWindowWatcher.openWindow,
+ /* aParent */ null,
+ 'chrome://greasemonkey/content/install.xul',
+ /* aName */ null,
+ 'chrome,centerscreen,modal,dialog,titlebar,resizable',
+ params);
+ GM_util.timeout(0, curriedOpenWindow);
+ }
+
+ if (rs.script) {
+ params = [rs, aBrowser, rs.script];
+ params.wrappedJSObject = params;
+ openDialog();
+ } else {
+ rs.onScriptMeta(function(aRemoteScript, aType, aScript) {
+ params = [rs, aBrowser, aScript];
+ params.wrappedJSObject = params;
+ openDialog();
+ });
+ }
+
+ rs.download(function(aSuccess, aType) {
+ if (!aSuccess) {
+ if ('script' == aType) {
+ // Failure downloading script; browse to it.
+ aService.ignoreNextScript();
+ // TODO: Test this in Firefox 3.
+ aBrowser.loadURI(aUrl, /* aReferrer */ null, /* aCharset */ null);
+ }
+ }
+ });
+}

0 comments on commit f8363f5

Please sign in to comment.