how to compile a greasemonkey script into .XPI for Firefox 2.0+

in Script development
Subscribe to how to compile a greasemonkey script into .XPI for Firefox 2.0+ 9 posts, 4 voices



Zorg Scriptwright

I'm trying to compile a greasemonkey script... found some compiler sites (i.e. http://www.letitblog.com/greasemonkey-compiler/) but they don't appear to work with Firefox v2.0.x. Any ideas for 2.0 compatible compilers?

thanks!

 
Michael Devore Scriptwright

The one at http://arantius.com/misc/greasemonkey/script-co... definitely works with Firefox 1.5 and 2.0.

 
Zorg Scriptwright

I tried that one, too... but after I install the XPI and restart, I keep getting a javascript alert saying "Error: missing } after function body".

Any ideas?

I have one simple function in my script:

function highlightElement(el) {
var setHighlight = 0;
// stylize the element
try {
el.style.border='2px #f00 dotted';
el.style.background='#FCC';
setHighlight = 1;
}
catch (err)
{
;
}
return(setHighlight);
}

...can't see why that would cause a problem. It works fine under the regular greasemonkey extension.

 
Henrik N Admin

Greasemonkey scripts are wrapped in a function call. Perhaps you've unbalanced {}s somewhere else in your code? Do you normally see error messages from GM scripts?

 
Zorg Scriptwright

I don't think there are any unbalanaced { }'s. I'm running this just fine in Greasmonkey (no js errors).

Here's the source ... it's not too long. Please see if you can spot any problems:

// ==UserScript==
// @name XXX Detector
// @namespace http://www.xxx.com
// @description Detects if a page is using xxx code
// @include *
// ==/UserScript==

function highlightElement(el, insertEmptyTag) {
var setHighlight = 0;
// stylize the element
try {
el.style.border='2px #f00 dotted';
el.style.background='#FCC';
setHighlight = 1;
if (insertEmptyTag && el.innerHTML.search(/^[ \n]*$/) != -1) {
// insert marker for empty divs
el.innerHTML = '<center><font>(empty)</font></center>';
}
}
catch (err)
{
;
}
return(setHighlight);
}

var scripts = document.getElementsByTagName( "script" );
var vendor = null;
var parseDivs = false;
var testAreas = 0;

for ( var i = 0; i < scripts.length; ++i ) {
var scriptEl = scripts[ i ];
// check both inline contents of a script block as well as its source (include file)
var codeTest = scriptEl.innerHTML + ';' + scriptEl.src;

if ( codeTest.search( /(^xxx|yyy|zzz)/i ) != -1 ) {
if (codeTest.search(/xxx/i) != -1) {
vendor = 'xxx';
parseDivs = true;
break;
} else if (codeTest.search(/yyy/i) != -1) {
vendor = 'yyy';
parseDivs = true;
break;
} else if (codeTest.search(/zzz/i) != -1) {
vendor = 'zzz';
parseDivs = true;
break;
}
}
}

if (vendor != null) {
if (parseDivs) {
// walk through the DOM and highlight xxx div's
var testZones = document.getElementsByTagName( "div" );

for ( var i = 0; i < testZones.length; ++i ) {
var testZone = testZones[ i ];

if (testZone.id.search(/^(xxx|yyy|zzz)/i) != -1) {
// count test areas, but only if they're not hidden
if (testZone.style.visibility != 'hidden' && testZone.style.display != 'none' && highlightElement(testZone, true)) {
testAreas++;
}
}
}
}

var messageDiv = document.createElement("div");
var messageText = vendor + ' code detected (' + testAreas + ' areas)';
messageDiv.id = "logo";
messageDiv.innerHTML = '

' +
' ' + messageText + ' ' +
'
';
window.status=messageText;
document.body.insertBefore( messageDiv, document.body.firstChild );
}

//alert('done');

 
znerp Scriptwright

I've not looked at your code for more than 10 seconds to glance down it, and I have a quick question -- why is 'messageDiv.innerHTML' (towards the end) spread over so many lines? Is this the way the forum's formatted it, because I'm not sure but that might cause problems if that's how it is in your script.

 
Zorg Scriptwright

The webboard reformatted it ... here it is again, this time HTML-escaped:

messageDiv.innerHTML = '<div style="position: absolute; left: 0px; top: 0px;' +
		'border: 2px dotted #F00; ' +
		'font-size: 14px; font-family: arial; background-color: #FCC; z-index: 1000;' +
		'color: #000; margin:0px; padding:3px; font-weight:bold;"%gt;' +
		' ' + messageText + ' ' +
		'</div>';

 
Michael Devore Scriptwright

Interesting problem. I discovered that the compiled version of your original MVT Vendor Detector script would work if a trailing newline was added to the source code, otherwise the error message you listed occurred on all sites. In other words, if you add a line after '//alert('done');', then the XPI will work properly on the Google test site listed in your script comment, highlighting several matches.

 
Zorg Scriptwright

Aha! Ok, that did the trick. Thanks!

Z