Skip to content

Commit

Permalink
Merge branch 'master' into issue-1111
Browse files Browse the repository at this point in the history
Conflicts:
	content/addons.js
  • Loading branch information
sizzlemctwizzle committed May 28, 2010
2 parents abbb213 + 900cd90 commit acb8767
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 61 deletions.
27 changes: 13 additions & 14 deletions components/greasemonkey.js
Expand Up @@ -120,17 +120,14 @@ var greasemonkeyService = {
},

domContentLoaded: function(wrappedContentWin, chromeWin) {
var unsafeWin = wrappedContentWin.wrappedJSObject;
var unsafeLoc = new XPCNativeWrapper(unsafeWin, "location").location;
var href = new XPCNativeWrapper(unsafeLoc, "href").href;
var scripts = this.initScripts(href, wrappedContentWin, chromeWin);
var url = wrappedContentWin.document.location.href;
var scripts = this.initScripts(url, wrappedContentWin, chromeWin);

if (scripts.length > 0) {
this.injectScripts(scripts, href, unsafeWin, chromeWin);
this.injectScripts(scripts, url, wrappedContentWin, chromeWin);
}
},


startup: function() {
var loader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Ci.mozIJSSubScriptLoader);
Expand Down Expand Up @@ -232,22 +229,21 @@ var greasemonkeyService = {
return this.config.getMatchingScripts(testMatch);
},

injectScripts: function(scripts, url, unsafeContentWin, chromeWin) {
injectScripts: function(scripts, url, wrappedContentWin, chromeWin) {
var sandbox;
var script;
var logger;
var console;
var storage;
var xmlhttpRequester;
var resources;
var safeWin = new XPCNativeWrapper(unsafeContentWin);
var safeDoc = safeWin.document;
var unsafeContentWin = wrappedContentWin.wrappedJSObject;

// detect and grab reference to firebug console and context, if it exists
var firebugConsole = this.getFirebugConsole(unsafeContentWin, chromeWin);

for (var i = 0; script = scripts[i]; i++) {
sandbox = new Components.utils.Sandbox(safeWin);
sandbox = new Components.utils.Sandbox(wrappedContentWin);

logger = new GM_ScriptLogger(script);

Expand All @@ -259,15 +255,17 @@ var greasemonkeyService = {
url);
resources = new GM_Resources(script);

sandbox.window = safeWin;
sandbox.window = wrappedContentWin;
sandbox.document = sandbox.window.document;
sandbox.unsafeWindow = unsafeContentWin;

// hack XPathResult since that is so commonly used
sandbox.XPathResult = Ci.nsIDOMXPathResult;

// add our own APIs
sandbox.GM_addStyle = function(css) { GM_addStyle(safeDoc, css) };
sandbox.GM_addStyle = function(css) {
GM_addStyle(wrappedContentWin.document, css);
};
sandbox.GM_log = GM_hitch(logger, "log");
sandbox.console = console;
sandbox.GM_setValue = GM_hitch(storage, "setValue");
Expand All @@ -276,14 +274,15 @@ var greasemonkeyService = {
sandbox.GM_listValues = GM_hitch(storage, "listValues");
sandbox.GM_getResourceURL = GM_hitch(resources, "getResourceURL");
sandbox.GM_getResourceText = GM_hitch(resources, "getResourceText");
sandbox.GM_openInTab = GM_hitch(this, "openInTab", safeWin, chromeWin);
sandbox.GM_openInTab = GM_hitch(
this, "openInTab", wrappedContentWin, chromeWin);
sandbox.GM_xmlhttpRequest = GM_hitch(xmlhttpRequester,
"contentStartRequest");
sandbox.GM_registerMenuCommand = GM_hitch(this,
"registerMenuCommand",
unsafeContentWin);

sandbox.__proto__ = safeWin;
sandbox.__proto__ = wrappedContentWin;

var contents = script.textContent;

Expand Down
4 changes: 4 additions & 0 deletions content/addons.css
@@ -0,0 +1,4 @@
.userscripts richlistitem[opType="needs-uninstall"] hbox.addon-optype,
.userscripts richlistitem[opType="needs-uninstall"] hbox.addon-description {
-moz-binding: url("chrome://greasemonkey/content/addons.xml#addon-needs-uninstall");
}
115 changes: 85 additions & 30 deletions content/addons.js
@@ -1,6 +1,6 @@
// Globals.
var GM_config = GM_getConfig();

var GM_uninstallQueue = {};
var GM_stringBundle = Components
.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService)
Expand All @@ -15,6 +15,7 @@ showView = function(aView) {
if ('userscripts' == aView) {
greasemonkeyAddons.showView();
} else {
greasemonkeyAddons.hideView();
_origShowView(aView);
}
};
Expand Down Expand Up @@ -52,9 +53,8 @@ var observer = {
// find the script's node in the listbox
var listbox = gExtensionsView;
var node;
var scriptId = script.namespace + script.name;
for (var i = 0; node = listbox.childNodes[i]; i++) {
if (node.getAttribute('addonId') == scriptId) {
if (node.getAttribute('addonId') == script.id) {
break;
}
}
Expand Down Expand Up @@ -92,16 +92,33 @@ window.addEventListener('load', function() {
if (stylishRadio) {
stylishRadio.addEventListener(
'command',
function() { gView = 'userstyles' },
function() {
greasemonkeyAddons.hideView();
gView = 'userstyles'
},
false);
}
}, false);

window.addEventListener('unload', function() {
for (var id in GM_uninstallQueue) {
GM_config.uninstall(GM_uninstallQueue[id]);
delete(GM_uninstallQueue[id]);
}
// Guarantee that the config.xml is saved to disk.
// Todo: This without dipping into private members.
GM_config._save(true);
}, false);

var greasemonkeyAddons = {
showView: function() {
if ('userscripts' == gView) return;

greasemonkeyAddons.hideView();

updateLastSelected('userscripts');
gView='userscripts';
document.documentElement.className += ' userscripts';

// Update any possibly modified scripts.
GM_config.updateModifiedScripts();
Expand Down Expand Up @@ -145,18 +162,23 @@ var greasemonkeyAddons = {
}
},

hideView: function() {
if ('userscripts' != gView) return;
document.documentElement.className =
document.documentElement.className.replace(/ *\buserscripts\b/, '');
},

listitemForScript: function(script) {
var item = document.createElement('richlistitem');
item.setAttribute('class', 'userscript');
// Fake this for now.
var id = script.namespace + script.name;
// Setting these attributes inherits the values into the same place they
// would go for extensions.
item.setAttribute('addonId', id);
item.setAttribute('addonId', script.id);
item.setAttribute('name', script.name);
item.setAttribute('description', script.description);
item.setAttribute('version', script.version);
item.setAttribute('id', 'urn:greasemonkey:item:'+id);
item.setAttribute('id', 'urn:greasemonkey:item:'+script.id);
item.setAttribute('isDisabled', !script.enabled);
// These hide extension-specific bits we don't want to display.
item.setAttribute('blocklisted', 'false');
Expand All @@ -166,6 +188,11 @@ var greasemonkeyAddons = {
item.setAttribute('providesUpdatesSecurely', 'true');
item.setAttribute('satisfiesDependencies', 'true');
item.setAttribute('type', nsIUpdateItem.TYPE_EXTENSION);

if (script.id in GM_uninstallQueue) {
item.setAttribute('opType', 'needs-uninstall');
}

return item;
},

Expand All @@ -180,13 +207,14 @@ var greasemonkeyAddons = {
var scripts = GM_config.scripts;
var selectedScriptId = gExtensionsView.selectedItem.getAttribute('addonId');
for (var i = 0, script = null; script = scripts[i]; i++) {
if (selectedScriptId == script.namespace + script.name) {
if (selectedScriptId == script.id) {
return script;
}
}
return null;
},

// Todo: Completely replace this with overlaid XBL, like UninstallCancel.
onAddonSelect: function(aEvent) {
// We do all this work here, because the elements we want to change do
// not exist until the item is selected.
Expand Down Expand Up @@ -230,6 +258,13 @@ var greasemonkeyAddons = {
button.setAttribute('tooltiptext', GM_string('Uninstall.tooltip'));
button.setAttribute('command', 'cmd_userscript_uninstall');
button.setAttribute('disabled', 'false');

button = item.ownerDocument.getAnonymousElementByAttribute(
item, 'command', 'cmd_cancelUninstall');
if (!button) return;
button.setAttribute('tooltiptext', GM_string('UninstallCancel.tooltip'));
button.setAttribute('command', 'cmd_userscript_uninstall_cancel');
button.setAttribute('disabled', 'false');
},

doCommand: function(command) {
Expand Down Expand Up @@ -272,6 +307,21 @@ var greasemonkeyAddons = {
greasemonkeyAddons.fillList();
break;
case 'cmd_userscript_uninstall':
GM_uninstallQueue[script.id] = script;
// Todo: This without dipping into private members?
script.needsUninstallEnabled = script._enabled;
script._enabled = false;
selectedListitem.setAttribute('opType', 'needs-uninstall');
break;
case 'cmd_userscript_uninstall_cancel':
delete(GM_uninstallQueue[script.id]);
// Todo: This without dipping into private members?
script._enabled = script.needsUninstallEnabled;
delete(script.needsUninstallDisabled);
selectedListitem.removeAttribute('opType');
break;
case 'cmd_userscript_uninstall_now':
delete(GM_uninstallQueue[script.id]);
GM_config.uninstall(script);
break;
}
Expand All @@ -284,6 +334,7 @@ var greasemonkeyAddons = {
return;
}

var selectedItem = gExtensionsView.selectedItem;
var popup = document.getElementById('addonContextMenu');
while (popup.hasChildNodes()) {
popup.removeChild(popup.firstChild);
Expand Down Expand Up @@ -311,30 +362,34 @@ var greasemonkeyAddons = {
popup.appendChild(menuitem);
}

addMenuItem('Edit', 'cmd_userscript_edit');
addMenuItem('Show Containing Folder', 'cmd_userscript_show');
if (script.enabled) {
addMenuItem('Disable', 'cmd_userscript_disable');
if ('needs-uninstall' == selectedItem.getAttribute('opType')) {
addMenuItem('UninstallCancel', 'cmd_userscript_uninstall_cancel');
addMenuItem('UninstallNow', 'cmd_userscript_uninstall_now');
} else {
addMenuItem('Enable', 'cmd_userscript_enable');
}
addMenuItem('Uninstall', 'cmd_userscript_uninstall');
addMenuItem('Edit', 'cmd_userscript_edit');
addMenuItem('Show Containing Folder', 'cmd_userscript_show');
if (script.enabled) {
addMenuItem('Disable', 'cmd_userscript_disable');
} else {
addMenuItem('Enable', 'cmd_userscript_enable');
}
addMenuItem('Uninstall', 'cmd_userscript_uninstall');

popup.appendChild(document.createElement('menuseparator'));
popup.appendChild(document.createElement('menuseparator'));

var selectedItem = gExtensionsView.selectedItem;
addMenuItem('Move Up', 'cmd_userscript_move_up',
!!selectedItem.previousSibling);
addMenuItem('Move Down', 'cmd_userscript_move_down',
!!selectedItem.nextSibling);
addMenuItem('Move To Top', 'cmd_userscript_move_top',
!!selectedItem.previousSibling);
addMenuItem('Move To Bottom', 'cmd_userscript_move_bottom',
!!selectedItem.nextSibling);

popup.appendChild(document.createElement('menuseparator'));

addMenuItem('Sort Scripts', 'cmd_userscript_sort',
gExtensionsView.itemCount > 1);
addMenuItem('Move Up', 'cmd_userscript_move_up',
!!selectedItem.previousSibling);
addMenuItem('Move Down', 'cmd_userscript_move_down',
!!selectedItem.nextSibling);
addMenuItem('Move To Top', 'cmd_userscript_move_top',
!!selectedItem.previousSibling);
addMenuItem('Move To Bottom', 'cmd_userscript_move_bottom',
!!selectedItem.nextSibling);

popup.appendChild(document.createElement('menuseparator'));

addMenuItem('Sort Scripts', 'cmd_userscript_sort',
gExtensionsView.itemCount > 1);
}
}
};
15 changes: 15 additions & 0 deletions content/addons.xml
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!DOCTYPE overlay SYSTEM "chrome://greasemonkey/locale/gm-addons.dtd">
<bindings id="addonUserscriptsBindings"
xmlns="http://www.mozilla.org/xbl"
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:xbl="http://www.mozilla.org/xbl"
>

<binding id="addon-needs-uninstall">
<content>
<xul:label value="&UninstallCancelDescription;" crop="end"/>
</content>
</binding>

</bindings>
3 changes: 3 additions & 0 deletions content/addons.xul
@@ -1,5 +1,6 @@
<?xml version="1.0"?>
<!DOCTYPE overlay SYSTEM "chrome://greasemonkey/locale/greasemonkey.dtd">
<?xml-stylesheet href="chrome://greasemonkey/content/addons.css" type="text/css"?>
<?xml-stylesheet href="chrome://greasemonkey/skin/addons.css" type="text/css"?>

<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
Expand All @@ -23,6 +24,8 @@
<command id="cmd_userscript_enable"/>
<command id="cmd_userscript_disable"/>
<command id="cmd_userscript_uninstall"/>
<command id="cmd_userscript_uninstall_cancel"/>
<command id="cmd_userscript_uninstall_now"/>

<command id="cmd_userscript_move_up"/>
<command id="cmd_userscript_move_down"/>
Expand Down
10 changes: 3 additions & 7 deletions content/browser.js
Expand Up @@ -130,26 +130,22 @@ GM_BrowserUI.contentLoad = function(e) {
var safeWin;
var unsafeWin;
var href;
var commander;

if (!GM_getEnabled()) {
return;
}
if (!GM_getEnabled()) return;

safeWin = e.target.defaultView;
unsafeWin = safeWin.wrappedJSObject;
href = safeWin.location.href;

if (GM_isGreasemonkeyable(href)) {
commander = this.getCommander(unsafeWin);

// if this content load is in the focused tab, attach the menuCommaander
if (unsafeWin == this.tabBrowser.selectedBrowser.contentWindow) {
var commander = this.getCommander(safeWin);
this.currentMenuCommander = commander;
this.currentMenuCommander.attach();
}

this.gmSvc.domContentLoaded({ wrappedJSObject: unsafeWin }, window);
this.gmSvc.domContentLoaded(safeWin, window);

GM_listen(unsafeWin, "pagehide", GM_hitch(this, "contentUnload"));
}
Expand Down

0 comments on commit acb8767

Please sign in to comment.