Skip to content

Commit

Permalink
Fixes bugs 8634 and 8831 (scripts not applied to background tabs or f…
Browse files Browse the repository at this point in the history
…rames)

as well as the other behavior described in http://www.mozdev.org/pipermail/greasemonkey/2005-January/000013.html

Additionally, upgrades now keep your old script configuration
  • Loading branch information
matthewkgray committed Jan 14, 2005
1 parent ec0b488 commit 93cdf35
Show file tree
Hide file tree
Showing 7 changed files with 448 additions and 453 deletions.
161 changes: 74 additions & 87 deletions src/content/browser.xul
Expand Up @@ -5,96 +5,83 @@
<script type="application/x-javascript" src="chrome://greasemonkey/content/greasemonkey.js" />
<script type="application/x-javascript">
<![CDATA[
window.addEventListener("load", function(e) {
window.addEventListener("load", greaseInit, false);
function greaseInit() {
var appcontent = document.getElementById("appcontent");
if(appcontent){
if(!appcontent.greased){
appcontent.greased = true;
appcontent.addEventListener("load", greaseLoad, true);
window.document.getElementById("userscript-tools-manage").addEventListener("command", function() {
window.openDialog("chrome://greasemonkey/content/manage.xul", "manager", "resizable,centerscreen,modal");
// not necessary to reload here i don't think, because userscript injection
// is only done at load time anyway, and then the config will be reloaded
}, true);
window.document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", function() {
var culprit = document.popupNode;
var contextItem = document.getElementById("install-userscript");
var contextSep = document.getElementById("install-userscript-sep");
contextItem.hidden =
contextSep.hidden = !(culprit.tagName.toLowerCase() == "a" &&
culprit.href.match(/\.user\.js(\?|$)/i) != null);
}, false);
window.document.getElementById("menu_ToolsPopup").addEventListener("popupshowing", function() {
var culprit = document.popupNode;
var installItem = window.document.getElementById("userscript-tools-install");
var disabled = !(window._content && window._content.location &&
window._content.location.href.match(/\.user\.js(\?|$)/i) != null);
installItem.setAttribute("disabled", disabled.toString());
}, false);
window.document.getElementById("install-userscript").addEventListener("command", function(e) {
new ScriptDownloader(document.popupNode.href).start();
}, false);
window.document.getElementById("userscript-tools-install").addEventListener("command", function(e) {
new ScriptDownloader(window._content.location.href).start();
}, false);
}
}
}
greaseLoad = function(e) {
var config = new Config();
var webProgress = getBrowser();
var browz = webProgress;
var scriptDownloader;
// this needs to be defined as an actual variable, not anonymously!
// addProgressListener only adds a weak reference, so if this is not
// a real variable, it will get GC'd and stop working at some non-
// deterministic time.
var listener = {
QueryInterface : function(aIID) { return this; },
config.load();
outer:
for (var i = 0; i < config.scripts.length; i++) {
var script = config.scripts[i];
if (script.enabled) {
for (var j = 0; j < script.includes.length; j++) {
var pattern = convert2RegExp(script.includes[j]);
if (pattern.test(e.originalTarget.location.href)) {
for (var k = 0; k < script.excludes.length; k++) {
pattern = convert2RegExp(script.excludes[k]);
if (pattern.test(e.originalTarget.location.href)) {
continue outer;
}
}
var elm = e.originalTarget.createElement("script");
elm.setAttribute("src", getScriptChrome(script.id));
e.originalTarget.body.appendChild(elm);
continue outer;
}
}
}
}
onLocationChange : function(){},
onProgressChange : function(){},
onSecurityChange : function(){},
onStatusChange : function(){},
onLinkIconAvailable : function(){},
onStateChange : function(webProgress, request, stateFlags, status) {
//dump(stateFlags + "\n");
if ((stateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP) &&
(status == 0)) {
config.load();
outer:
for (var i = 0; i < config.scripts.length; i++) {
var script = config.scripts[i];
if (script.enabled) {
for (var j = 0; j < script.includes.length; j++) {
var pattern = convert2RegExp(script.includes[j]);
if (pattern.test(webProgress.DOMWindow.location.href)) {
for (var k = 0; k < script.excludes.length; k++) {
pattern = convert2RegExp(script.excludes[k]);
if (pattern.test(webProgress.DOMWindow.location.href)) {
continue outer;
}
}
var elm = webProgress.DOMWindow.document.createElement("script");
elm.setAttribute("src", getScriptChrome(script.id));
webProgress.DOMWindow.document.body.appendChild(elm);
continue outer;
}
}
}
}
}
}
};
webProgress.addProgressListener(listener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
window.document.getElementById("userscript-tools-manage").addEventListener("command", function() {
window.openDialog("chrome://greasemonkey/content/manage.xul", "manager", "resizable,centerscreen,modal");
// not necessary to reload here i don't think, because userscript injection
// is only done at load time anyway, and then the config will be reloaded
}, true);
window.document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", function() {
var culprit = document.popupNode;
var contextItem = document.getElementById("install-userscript");
var contextSep = document.getElementById("install-userscript-sep");
contextItem.hidden =
contextSep.hidden = !(culprit.tagName.toLowerCase() == "a" &&
culprit.href.match(/\.user\.js(\?|$)/i) != null);
}, false);
window.document.getElementById("menu_ToolsPopup").addEventListener("popupshowing", function() {
var culprit = document.popupNode;
var installItem = window.document.getElementById("userscript-tools-install");
var disabled = !(window._content && window._content.location &&
window._content.location.href.match(/\.user\.js(\?|$)/i) != null);
installItem.setAttribute("disabled", disabled.toString());
}, false);
window.document.getElementById("install-userscript").addEventListener("command", function(e) {
new ScriptDownloader(document.popupNode.href).start();
}, false);
window.document.getElementById("userscript-tools-install").addEventListener("command", function(e) {
new ScriptDownloader(window._content.location.href).start();
}, false);
function parseArgs(href) {
var qsStartPos = href.lastIndexOf("?");
Expand All @@ -114,7 +101,7 @@
return vargs;
}
}, false);
};
]]>
</script>

Expand All @@ -129,4 +116,4 @@
<menuitem id="userscript-tools-manage" position="11" accesskey="U" label="Manage User Scripts..." />
</menupopup>

</overlay>
</overlay>
8 changes: 6 additions & 2 deletions src/content/greasemonkey.js
Expand Up @@ -21,7 +21,11 @@ function Config() {
this.load = function() {
var doc = document.implementation.createDocument("", "", null);
doc.async = false;
doc.load(getScriptChrome("config.xml"));
try {
doc.load(getScriptChrome("config.xml"));
} catch (exc) {
doc.load(getScriptChrome("default-config.xml"));
}

var nodes = document.evaluate("/UserScriptConfig/Script", doc, null, 0, null);

Expand Down Expand Up @@ -370,4 +374,4 @@ function dbg(o) {
}

alert(s);
}
}
165 changes: 84 additions & 81 deletions src/content/install.xul
@@ -1,81 +1,84 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xul-overlay href="chrome://greasemonkey/content/pages-overlay.xul"?>

<window
id="manage-window"
title="Install User Script"
orient="vertical"
style="max-width:400px;"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

<script type="application/x-javascript" src="chrome://greasemonkey/content/greasemonkey.js" />
<script type="application/x-javascript"><![CDATA[
window.addEventListener("load", function() {
var pagesControl = new PagesControl(document.getElementById("pages-control"));
var script = window.arguments[0];
var file = window.arguments[1];
var result = window.arguments[2];
var status = document.getElementById("status");
var config = new Config();
var request;
config.load();
document.getElementById("ctlHeader").setAttribute("title", script.name);
document.getElementById("ctlDescription").textContent = script.description;
if (script != null) {
pagesControl.populate(script);
document.getElementById("btnOK").addEventListener("command", function() {
var newDir = getScriptDir();
var existingIndex = config.find(script.namespace, script.name);
var existingFile = null;
var oldScripts = new Array(config.scripts);
if (existingIndex > -1) {
existingFile = getScriptFile(config.scripts[existingIndex].id);
config.scripts.splice(existingIndex, 1);
}
try {
file.moveTo(newDir, script.id);
config.scripts.push(script);
config.save();
if (existingFile != null) {
existingFile.remove(false);
}
result.value = true;
window.close();
}
catch (e) {
config.scripts = oldScripts;
alert("Could not save script. " + (e ? e : ""));
}
}, false);
document.getElementById("btnCancel").addEventListener("command", function() {
if (request) request.abort();
window.close();
}, false);
}
}, false);
]]></script>

<vbox style="margin:1em;">
<dialogheader id="ctlHeader" title="" />
<description id="ctlDescription" style="margin-top:.5em; margin-bottom:.5em; padding-left:2px; padding-right:2px;" />
<vbox id="pages-control" />
<spacer flex="1" style="min-height:2em" />
<hbox align="center" style=" height:3em;">
<description id="status" flex="1" style="font-weight:bold;">Press OK to confirm these settings and install the user script...</description>
<button id="btnOK" label="OK" default="true" />
<button id="btnCancel" label="Cancel" />
</hbox>
</vbox>
</window>
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xul-overlay href="chrome://greasemonkey/content/pages-overlay.xul"?>

<window
id="manage-window"
title="Install User Script"
orient="vertical"
style="max-width:400px;"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

<script type="application/x-javascript" src="chrome://greasemonkey/content/greasemonkey.js" />
<script type="application/x-javascript"><![CDATA[
window.addEventListener("load", function(ev) {
var pagesControl = new PagesControl(document.getElementById("pages-control"));
var script = window.arguments[0];
var file = window.arguments[1];
// alert("install.xul\n"+ev.originalTarget.location+"\n"+file);
var result = window.arguments[2];
var status = document.getElementById("status");
var config = new Config();
var request;
config.load();
document.getElementById("ctlHeader").setAttribute("title", script.name);
document.getElementById("ctlDescription").textContent = script.description;
if (script != null) {
pagesControl.populate(script);
document.getElementById("btnOK").addEventListener("command", function() {
// alert("ok button");
var newDir = getScriptDir();
var existingIndex = config.find(script.namespace, script.name);
var existingFile = null;
var oldScripts = new Array(config.scripts);
if (existingIndex > -1) {
existingFile = getScriptFile(config.scripts[existingIndex].id);
config.scripts.splice(existingIndex, 1);
}
try {
file.moveTo(newDir, script.id);
config.scripts.push(script);
config.save();
if (existingFile != null) {
existingFile.remove(false);
}
result.value = true;
window.close();
}
catch (e) {
config.scripts = oldScripts;
alert("Could not save script. " + (e ? e : ""));
}
}, false);
document.getElementById("btnCancel").addEventListener("command", function() {
// alert("cancel button");
if (request) request.abort();
window.close();
}, false);
}
}, false);
]]></script>

<vbox style="margin:1em;">
<dialogheader id="ctlHeader" title="" />
<description id="ctlDescription" style="margin-top:.5em; margin-bottom:.5em; padding-left:2px; padding-right:2px;" />
<vbox id="pages-control" />
<spacer flex="1" style="min-height:2em" />
<hbox align="center" style=" height:3em;">
<description id="status" flex="1" style="font-weight:bold;">Press OK to confirm these settings and install the user script...</description>
<button id="btnOK" label="OK" default="true" />
<button id="btnCancel" label="Cancel" />
</hbox>
</vbox>
</window>

0 comments on commit 93cdf35

Please sign in to comment.