Troubleshooting (Script Authors): Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
No edit summary
(Moved "Changes don't take effect" here from FAQ. Made section headers <h2>, not <h1>.)
Line 5: Line 5:
__TOC__
__TOC__


= When using GM_xmlhttpRequest or setTimeout, variables change before the callback runs =
== Changes don't take effect when editing a script ==
 
You may be editing your original copy of the script, not the installed copy.
Once you create and install a script, you need to follow [[FAQ#How_do_I_edit_a_script_I.27m_working_on.3F|these steps]] to make sure you're editing the installed, active copy.
 
Also, changing the [[include and exclude rules]] in the [[metadata block]] of the installed script does not do anything, as the script metadata is only accessed during installation. The script must be re-installed (or [[config.xml]] edited manually) for these changes to take.
 
 
== Variables change before a GM_xmlhttpRequest or setTimeout callback runs ==


Problem example:
Problem example:
Line 61: Line 69:
   }
   }


= Links =
== Links ==


* http://greaseblog.blogspot.com/2005/12/troubleshooting-064.html
* http://greaseblog.blogspot.com/2005/12/troubleshooting-064.html

Revision as of 20:48, 13 April 2007

The article Avoid Common Pitfalls in Greasemonkey covers some of the most common problems people come across when writing user scripts.

If your problem is not listed, please ask on the mailing list.

Changes don't take effect when editing a script

You may be editing your original copy of the script, not the installed copy. Once you create and install a script, you need to follow these steps to make sure you're editing the installed, active copy.

Also, changing the include and exclude rules in the metadata block of the installed script does not do anything, as the script metadata is only accessed during installation. The script must be re-installed (or config.xml edited manually) for these changes to take.


Variables change before a GM_xmlhttpRequest or setTimeout callback runs

Problem example:

 for (var i=0; i < document.links.length; i++) {
   var link = document.links[i];
 
   GM_xmlhttpRequest({
     method: "GET",
     url: 'http://example.com/lookup?url=' + link.href,
     onload: function(result) {
       link.href = result.responseText;
     }
   });
 }

The request is asynchronous, meaning the rest of the code doesn't wait for it to complete. The for keeps running as the request loads. When the request for the first link on the page completes and the onload callback function runs, the link variable might point to a different link altogether, typically the last one (since the for loop completes much quicker than the HTTP requests).

One solution is to pass those values that you want unchanged into a function surrounding the request, as arguments. Then they will be in a different scope, and will no longer be changed from the outside.

Solution example with named function:

 function do_it(link_inside) {
   GM_xmlhttpRequest({
     method: "GET",
     url: 'http://example.com/lookup?url=' + link_inside.href,
     onload: function(result) {
       link_inside.href = result.responseText;
     }
   });
 }
 
 for (var i=0; i < document.links.length; i++) {
   var link = document.links[i];
 
   do_it(link);
 }

Solution example with anonymous function:

 for (var i=0; i < document.links.length; i++) {
   var link = document.links[i];
 
   (function (link_inside) {
 
     GM_xmlhttpRequest({
       method: "GET",
       url: 'http://example.com/lookup?url=' + link_inside.href,
       onload: function(result) {
         link_inside.href = result.responseText;
       }
     });
 
   })(link);
 }

Links