Greasemonkey script compiler is showing an error - javascript

I have script like this
// ==UserScript==
// #name messi
// #namespace http://messi.com
// #description example script to alert "barcelona" on userscripts page
// #include http://userscripts.org/*
// ==/UserScript==
alert('barcelona');
this script works with Greasemonkey.
I compiled this script using this link:
https://arantius.com/misc/greasemonkey/script-compiler.php.
After compilation I added add-on to Firefox, now it showing:
Error: Illegal operation on WrappedNative prototype objec error .
whats wrong ?

The user script compiler wasn't updated in quite some time and the output add-ons it produces stopped working in recent Firefox versions IIRC.
If you want to create Firefox add-ons from userscripts, you should consider using the Add-on SDK and making your user-script a page mod. That is what the addons.mozilla.org editor team is recommending, anyway.
There are other alternatives, such as Scriptify.

Related

Greasemonkey swallows JS errors without logging them [duplicate]

I am having issues with this very basic Greasemonkey script, most likely with the metadata configuration.
Here is the full source of the basic file
// ==UserScript==
// #name Google Hello
// #namespace https://google.com
// #description Basic Google Hello
// #include *
// #version 1
// ==/UserScript==
alert("hi google!");
This script should run when I access Google.com, but the alert is not popping up. What is the issue?
I am attempting to run this script on Ubuntu with Firefox.
If alerts() are not firing, chances are you may have clicked Firefox's Prevent this page from creating additional dialogs option, or set a browser preference (older versions of Firefox), or Firefox may have become unstable in memory.
Universal Greasemonkey debug steps:
(With one step added for problems with alert().)
First make sure that the script is even firing for the page in question.
While browsing that page, click on the down-triangle next to the Greasemonkey icon (Alternatively, you can Open Tools -> Greasemonkey on the Firefox menu.) and verify that the expected script name appears and is checked. EG:
See if there are any relevant messages/errors on Firefox's Browser Console.
Activate the console by pressing CtrlShiftJ, or equivalent.
Here's a screenshot showing how both messages and errors appear in the Browser Console -- caused by both the web page and the Greasemonkey script:
Open about:config, search for capability.policy.default.Window.alert and delete or reset the value, if it is found.
Uninstall the Greasemonkey script.
Completely clear the browser cache.
Shutdown Firefox completely. Use Task Manager, or equivalent, to verify that there is no Firefox thread/task/process in memory.
Restart Firefox.
Install the Greasemonkey script afresh.
If it still doesn't work, create a new Firefox profile or try a different computer altogether.
Additional issues:
Please supply your versions of three things: (1) The OS, (2) Firefox, (3) Greasemonkey or Tampermonkey or Scriptish, etc.
#include * means that the script will fire for every page! This is almost always a poor practice. (There are some exceptions, but your case is not one.)
#namespace does not control where the page runs. The only thing #namespace does is allow more than one script to have the same name (as long as their #namespaces are different). See the #namespace documentation.
Avoid using alert() for debugging. It's annoying and can mask timing problems.
Use console.log(). You can see the results, and helpful error messages (hint, hint) on the Browser Console.
Google almost always uses/redirects to www.google.com (For English USA users). So, // #include  https://google.com will almost never work like you want.
Recommend you use:
// #match *://www.google.com/*
as a starting point.
In Firefox Greasemonkey, you can also use the magic .tld to support most of Google's international domains, like so:
// #include http://www.google.tld/*
// #include https://www.google.tld/*
Use both lines. Note that this does not perform as well as the #match line does. So, if you only care about one nation/locale, just use #match.
Putting it all together:
Uninstall your script.
Restart Firefox.
Install this script:
// ==UserScript==
// #name Google Hello
// #namespace John Galt
// #description Basic Google Hello
// #match *://www.google.com/*
// #version 1
// #grant none
// ==/UserScript==
console.log ("Hi Google!");
Visit Google and note the results on Firefox's Browser Console.
If there is still a problem, follow all of the debug steps above.
If there is still a problem, Open a new question and supply ALL of the following:
The three versions, mentioned above.
The relevant errors and messages you get on the Browser Console.
The exact code and steps needed to duplicate the problem. Make an MCVE for this!
A short summary of what you have tried to solve the problem.

Using document.evaluate in Greasemonkey [duplicate]

I am having issues with this very basic Greasemonkey script, most likely with the metadata configuration.
Here is the full source of the basic file
// ==UserScript==
// #name Google Hello
// #namespace https://google.com
// #description Basic Google Hello
// #include *
// #version 1
// ==/UserScript==
alert("hi google!");
This script should run when I access Google.com, but the alert is not popping up. What is the issue?
I am attempting to run this script on Ubuntu with Firefox.
If alerts() are not firing, chances are you may have clicked Firefox's Prevent this page from creating additional dialogs option, or set a browser preference (older versions of Firefox), or Firefox may have become unstable in memory.
Universal Greasemonkey debug steps:
(With one step added for problems with alert().)
First make sure that the script is even firing for the page in question.
While browsing that page, click on the down-triangle next to the Greasemonkey icon (Alternatively, you can Open Tools -> Greasemonkey on the Firefox menu.) and verify that the expected script name appears and is checked. EG:
See if there are any relevant messages/errors on Firefox's Browser Console.
Activate the console by pressing CtrlShiftJ, or equivalent.
Here's a screenshot showing how both messages and errors appear in the Browser Console -- caused by both the web page and the Greasemonkey script:
Open about:config, search for capability.policy.default.Window.alert and delete or reset the value, if it is found.
Uninstall the Greasemonkey script.
Completely clear the browser cache.
Shutdown Firefox completely. Use Task Manager, or equivalent, to verify that there is no Firefox thread/task/process in memory.
Restart Firefox.
Install the Greasemonkey script afresh.
If it still doesn't work, create a new Firefox profile or try a different computer altogether.
Additional issues:
Please supply your versions of three things: (1) The OS, (2) Firefox, (3) Greasemonkey or Tampermonkey or Scriptish, etc.
#include * means that the script will fire for every page! This is almost always a poor practice. (There are some exceptions, but your case is not one.)
#namespace does not control where the page runs. The only thing #namespace does is allow more than one script to have the same name (as long as their #namespaces are different). See the #namespace documentation.
Avoid using alert() for debugging. It's annoying and can mask timing problems.
Use console.log(). You can see the results, and helpful error messages (hint, hint) on the Browser Console.
Google almost always uses/redirects to www.google.com (For English USA users). So, // #include  https://google.com will almost never work like you want.
Recommend you use:
// #match *://www.google.com/*
as a starting point.
In Firefox Greasemonkey, you can also use the magic .tld to support most of Google's international domains, like so:
// #include http://www.google.tld/*
// #include https://www.google.tld/*
Use both lines. Note that this does not perform as well as the #match line does. So, if you only care about one nation/locale, just use #match.
Putting it all together:
Uninstall your script.
Restart Firefox.
Install this script:
// ==UserScript==
// #name Google Hello
// #namespace John Galt
// #description Basic Google Hello
// #match *://www.google.com/*
// #version 1
// #grant none
// ==/UserScript==
console.log ("Hi Google!");
Visit Google and note the results on Firefox's Browser Console.
If there is still a problem, follow all of the debug steps above.
If there is still a problem, Open a new question and supply ALL of the following:
The three versions, mentioned above.
The relevant errors and messages you get on the Browser Console.
The exact code and steps needed to duplicate the problem. Make an MCVE for this!
A short summary of what you have tried to solve the problem.

Error: Permission denied to access property 'handler'

I have a greasemonkey script for Firefox, which yesterday was working perfectly. I tried using it today (no code was modified) and I noticed that it stopped working. Upon further inspection, the script is now throwing the following error:
Error: Permission denied to access property 'handler'
This error is being thrown in the following block of code:
$('body').click(function() {
// code here
});
This error magically started happening today when the script was working just fine yesterday. I'm not understanding why this error is happening when just trying to do something so basic such as adding an event handler in jQuery.
My script uses jQuery which is already being used in the page the script executes on, so I used this code to make it accessible to GM:
var $ = unsafeWindow.jQuery;
For reference if need be, here are the following Greasemonkey functions I use in my script:
// #grant GM_getResourceText
// #grant GM_addStyle
// #grant GM_xmlhttpRequest
// #grant GM_getResourceURL
I have tried researching this error and I can't find any answer. All of the questions that look like they might be helpful involve iframes and there is not a single iframe to be found in my code or the website it's run on. I've also tried deleting and re-installing the script and that didn't fix the problem.
Greasemonkey 2.0 has just been pushed to all Firefox browsers set to auto-update. (GM 2 was released on June 17, 2014, but it can take a few weeks to get through the review process.)
Greasemonkey 2.0 radically changed unsafeWindow handling:
Backwards incompatible changes:
For stability, reliability, and security the privileged sandbox has been updated to match the new changes to unsafeWindow for the Add-on SDK. In order to write values to unsafeWindow you will need to use the new methods cloneInto(), exportFunction(), and/or createObjectIn().
The #grant none mode is now the default, and grants will no longer be implied when not explicitly provided. See the post Sandbox API Changes in Greasemonkey 2.0 for more detail.
Ordinarily, to spot-access a page function or variable, you could switch to the new methods but, in your case you are using var $ = unsafeWindow.jQuery; -- which was always a bad practice.
jQuery is a special case and cloning it back and forth is going to break things.
#require jQuery instead, EG:
// ==UserScript==
// #name _YOUR_SCRIPT_NAME
// #include http://YOUR_SERVER.COM/YOUR_PATH/*
// #require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// #grant GM_getResourceText
// #grant GM_addStyle
// #grant GM_xmlhttpRequest
// #grant GM_getResourceURL
// ==/UserScript==
...
You're using unsafeWindow – that, as the name suggested, is not necessary "safe" to use – the problem probably relies there; a change was made in Firefox about objects across compartments:
https://blog.mozilla.org/addons/2014/04/10/changes-to-unsafewindow-for-the-add-on-sdk/
The blog post mention Add-on SDK, but the changes is in the platform, so it will affect Greasemonkey too.
So you basically try to get an object from one compartment (jQuery, from "unsafeWindow") and use in your greasemonkey sandbox. The way you're doing now probably can't work anymore. You can try to use the API mentioned in the article, but I'm afraid that a whole library like jQuery could have some issue to be cloned. In fact, the best way is probably load jQuery also in your Greasemonkey compartment instead of reuse the one from the page's one.
The error probably started "magically" 'cause you have updated your version of Firefox – or it gets the autoupdated.
This page explains how to load jQuery in a Greasemonkey script: http://wiki.greasespot.net/Third-Party_Libraries
The relevant parts are:
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
...
this.$ = this.jQuery = jQuery.noConflict(true);
According to the docs, jQuery.noConflict() will make sure the version of jQuery for your script won't interfere with the page.
See also: jQuery in Greasemonkey 1.0 conflicts with websites using jQuery

Calling a function before the document loads up in JS

My friend asks me to do this.
He needs to prevent his children from going to certain websites.
He has tamper monkey installed with chrome. I have to make a script in tamper-monkey so that when it reaches the website it will change the web content.
The source code is:
// ==UserScript==
// #name My Fancy New Userscript
// #namespace http://use.i.E.your.homepage/
// #version 0.1
// #description enter something useful
// #match http://www.harmful-website.com/*
// #copyright 2012+, You
// ==/UserScript==
document.write("<b>You are not allowed to visit this site</b>");
This script works only after the web-site is loaded full. But his children stop the loading of website in the middle and they are able to view a part of it.
Even document.onload=function(){document.write("...");} works after load. Are there any way to make the script run before the document is loaded i.e. Immediately after the web address is typed on the address bar or hyperlink is clicked.
Your code will work, you just need to set #run-at document-startDoc, like so:
// ==UserScript==
// #name site blocker
// #match http://www.harmful-website.com/*
// #run-at document-start
// ==/UserScript==
document.write ("<b>You are not allowed to visit this site</b>");
Important:
This will work, for now, on Chrome and with Tampermonkey, but it does not work on other browsers. For example, on Firefox, the document.write call will throw the error:
Error: The operation is insecure.
(But the page will still be completely blank.)
Although a userscript like this will work (mostly); it is a klugey, brittle, low performance approach and is easily defeated. Here are just a few ways that are better, faster, easier, and harder for savvy kids to kibosh:
Use your home router's siteblock and/or parental controls. Almost every router and/or modem has this feature, these days.
Install one of the many extensions that do this kind of blocking.
Block the site with an Adblock rule.
Block the site with the machine's hosts file.
Use a proxy server.
Search on Super User for more options.
Just call the function right after you define it. Like in the header or somehwere.
However, you need to consider if the function has actually everything it requires, like HTML elements on the page if it access any of them, as those won't necessarily be loaded when calling your function.
HTML file is parsed row by row. So if your write your < script > just after < html > tag it will be executed before anything else.

Greasemonkey #require jQuery not working "Component not available"

I've seen the other question on here about loading jQuery in a Greasemonkey. Having tried that method, with this require statement inside my ==UserScript== tags:
// #require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
I still get the following error message in Firefox's error console:
Error: Component is not available
Source File: file:///Users/greg/Library/Application%20Support/
Firefox/Profiles/xo9xhovo.default/gm_scripts/myscript/jquerymin.js
Line: 36
This stops my greasemonkey code from running. I've made sure I included the #require for jQuery and saved my js file before installing it, as required files are only loaded on installation.
Code:
// ==UserScript==
// #name My Script
// #namespace http://www.google.com
// #description My test script
// #include http://www.google.com
// #require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// ==/UserScript==
GM_log("Hello");
I have Greasemonkey 0.8.20091209.4 installed on Firefox 3.5.7 on my Macbook Pro, Leopard (10.5.8). I've cleared my cache (except cookies) and have disabled all other plugins except Flashblock 1.5.11.2, Web Developer 1.1.8 and Adblock Plus 1.1.3.
My config.xml with my Greasemonkey script installed:
<UserScriptConfig>
<Script filename="myscript.user.js" name="My Script"
namespace="http://www.google.com" description="My test script" enabled="true"
basedir="myscript">
<Include>http://www.google.com</Include>
<Require filename="jquerymin.js"/>
</Script>
I can see jquerymin.js sat in the gm_scripts/myscript/ directory.
Additionally, is it common for this error to occur in the console when installing a Greasemonkey script?
Error: not well-formed
Source File: file:///Users/Greg/Documents/myscript.user.js
Line: 1, Column: 1
Source Code:
// ==UserScript==
I found a non-ideal way to use it with jQuery 1.4.1 -- this seems to fix it. It's the new browser sniffing that seems to "break" it.
jquery-1.4.1.min.js:
[old] 36: var o=r.createElement("div");n="on"+n;var m=n in o;
[new] 36: var o=r.createElement("div");n="on"+n;var m=true;
jquery-1.4.1.js
[old] 934: var isSupported = (eventName in el);
[new] 934: var isSupported = true;
Ok, so i looked into this a bit more deeper. I used your script exactly, but used our JQuery version, making it look like this:
// ==UserScript==
// #name My Script
// #namespace http://www.google.com
// #description My test script
// #include http://www.google.se/*
// #include http://www.dn.se/*
// #require http://myserver/jquery-1.3.2.js
// ==/UserScript==
GM_log("Hello");
This works just fine for me, my guess, the JQuery up on the google api is missing some functions. Because this code above, works just fine. Also note the /* at the end of each url, please include that.
Try another JQuery and change the urls and it should world properly.
I was stumbling around trying to deal with this issue with GM 0.8 and jquery 1.4.2 and found this: http://forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-generates-an-error
It looks to me like the definitive answer to the question and how to work around it. The workaround worked for me.
Good news and updating all postings:
The above patch allowed pre-1.5.2 jQuery versions to be run in Greasemonkey scripts, but fortunately the patch is no longer required if you use the current jQuery 1.5.2 version.
I checked its code and noticed that the eventSupported function code in jQuery
var eventSupported = function(eventName) { ... }
has been updated with the consequence that unpatched jQuery 1.5.2 now runs in Greasemonkey 0.9.2.
Patch for jquery-1.4.3.min.js
[old] line 41
u.createElement("div");s="on"+s;var
B=s in v; [new] line 41
u.createElement("div");s="on"+s;var
B=true;
The #require attribute doesn't work correctly in Greasemonkey and jQuery...this same error can occur in FireBug as well.
An alternative is to include jQuery in the page via Greasemonkey by creating the script tag. Here's how to do that.
Not entirely true, it seems like jQuery 1.4 tries to detect something using a call that just doesn't work in the greasemonkey environment. #require does normally work as it should.
So reverting to 1.3.2 does do the trick, but I'd rather find a solution that lets me use 1.4.
btw, I use this, slightly different:
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js
Here's a minified version of jQuery 1.4.4 for Greasemonkey:
http://userscripts.org/scripts/show/92329
Hope it helps,
yah

Categories