Troubleshooting (Script Authors): Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
Marti (talk | contribs)
m →‎Form POST data: Marked as good example
Update for 4.0
 
(26 intermediate revisions by 11 users not shown)
Line 1: Line 1:
The article [http://www.oreillynet.com/pub/a/network/2005/11/01/avoid-common-greasemonkey-pitfalls.html Avoid Common Pitfalls in Greasemonkey] covers some of the most common problems people come across when writing [[user script]]s.
If your problem is not listed, try [[Getting Help]].
 
If your problem is not listed, please ask on the [[mailing list]].


__TOC__
__TOC__
== 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.
Note: You can change the [[include and exclude rules]] in the Manage User Scripts window and those changes will take effect.


== Variables change before a GM_xmlhttpRequest or setTimeout callback runs ==
== Variables change before a GM_xmlhttpRequest or setTimeout callback runs ==
Line 18: Line 7:
Problem example:
Problem example:


{{Bad example|1=<pre style="border: none; margin: inherit;">for (var i = 0; i < document.links.length; i++) {
<pre class='sample-bad'>
for (var i = document.links.length - 1; i >= 0; --i) {
   var link = document.links[i];
   var link = document.links[i];
    
    
   GM_xmlhttpRequest({
   GM.xmlHttpRequest({
     method:"GET",
     method: "GET",
     url:"&#104;ttp://example.com/lookup?url=" + link.href,
     url: "http://example.com/lookup?url=" + link.href,
     onload:function(result) {
     onload: function(result) {
       link.href = result.responseText;
       link.href = result.responseText;
     }
     }
   });
   });
}</pre>}}
}</pre>


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


Solution example with named function:
Solution example with named function:
<code><pre>
<pre class='sample-good'>
function do_it(link_inside) {
function do_it(link_inside) {
   GM_xmlhttpRequest({
   GM_xmlhttpRequest({
     method:"GET",
     method: "GET",
     url:"&#104;ttp://example.com/lookup?url=" + link_inside.href,
     url: "http://example.com/lookup?url=" + link_inside.href,
     onload:function(result) {
     onload: function(result) {
       link_inside.href = result.responseText;
       link_inside.href = result.responseText;
     }
     }
Line 46: Line 36:
}
}
    
    
for (var i = 0; i < document.links.length; i++) {
for (var i = document.links.length - 1; i >= 0; --i) {
   var link = document.links[i];
   var link = document.links[i];
 
   do_it(link);
   do_it(link);
}
}
</pre></code>
</pre>
 
Solution example with anonymous function:
Solution example with anonymous function:


<code><pre>
<pre class='sample-good'>
for (var i = 0; i < document.links.length; i++) {
for (var i = document.links.length - 1; i >= 0; --i) {
   var link = document.links[i];
   var link = document.links[i];
    
    
   (function (link_inside) {
   (function (link_inside) {
 
     GM_xmlhttpRequest({
     GM_xmlhttpRequest({
       method:"GET",
       method: "GET",
       url:"&#104;ttp://example.com/lookup?url=" + link_inside.href,
       url: "http://example.com/lookup?url=" + link_inside.href,
       onload:function(result) {
       onload: function(result) {
         link_inside.href = result.responseText;
         link_inside.href = result.responseText;
       }
       }
     });
     });
 
   })(link);
   })(link);
}
}
</pre></code>
</pre>


== Component not Available errors ==
Solution example with iteration in the "onload" function. Note that this processes each element one by one.


These are usually caused by attempting to access an element's event handlers (<code>onclick</code>, <code>onkeydown</code>, etc.) directly, rather than through <code>addEventListener</code>. GreaseMonkey [[Version_history#0.5 |0.5]] and above require the use of addEventListener.  For details, see [[XPCNativeWrapper]].
<pre class='sample-good'>
(function getNext(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;
      if (--i >= 0) {
        getNext(i);
      }
    }
  });
})(document.links.length - 1);
</pre>


== Form POST data ==
== Form POST data ==


When using [[GM_xmlhttpRequest]] to POST form data, remember to add the '''Content-Type''' parameter:
When using [[GM.xmlHttpRequest]] to POST form data, remember to add the '''Content-Type''' parameter:


{{Good example |1=<pre style="border: none; margin: inherit;">
<pre class='sample-good'>
GM_xmlhttpRequest({
GM_xmlhttpRequest({
   method: "POST",
   method: "POST",
   url: myurl,
   url: myurl,
   headers:{
   headers: {
    "User-Agent": navigator.userAgent,
     "Content-Type": "application/x-www-form-urlencoded"
     "Content-Type": "application/x-www-form-urlencoded"
   },
   },
Line 91: Line 92:
   onload: function(res) { /* Some code */ }
   onload: function(res) { /* Some code */ }
});
});
</pre>}}
</pre>
 
== Character set ==
 
Various issues can arise when working in the wrong character set. In general, writers are advised to use the standard utf-8 format, which isn't supported in the latest version of Windows Notepad. Other workarounds include using JavaScript to parse characters which would otherwise be invalid in other formats, from integer values.
 
<pre class='sample-good'>
var values = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33];
alert(String.fromCharCode.apply(String, values));
</pre>


== Links ==
<!-- 4.0 + files = fail
== Installing from /tmp ==


* http://greaseblog.blogspot.com/2005/12/troubleshooting-064.html
If you try to install a user script located in the /tmp directory, Greasemonkey will not bring up the install dialog, and the Install button at the top of the page will not do anything. To solve this, simply move the .user.js file to another directory, such as your home directory.
* http://dunck.us/collab/GreaseMonkeyUserScripts#head-010b85ad56b34c34c7c2a3b2436c740e30428ed5
-->
* http://dunck.us/collab/GreaseMonkeyUserScripts#head-4ac4d1e80f8bbd66bf4f1fbea77ea2390b6a2870
* http://www.brockman.se/writing/method-references.html.utf8

Latest revision as of 16:20, 3 November 2017

If your problem is not listed, try Getting Help.

Variables change before a GM_xmlhttpRequest or setTimeout callback runs

Problem example:

for (var i = document.links.length - 1; i >= 0; --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 = document.links.length - 1; i >= 0; --i) {
  var link = document.links[i];
  do_it(link);
}

Solution example with anonymous function:

for (var i = document.links.length - 1; i >= 0; --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);
}

Solution example with iteration in the "onload" function. Note that this processes each element one by one.

(function getNext(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;
      if (--i >= 0) {
        getNext(i);
      }
    }
  });
})(document.links.length - 1);

Form POST data

When using GM.xmlHttpRequest to POST form data, remember to add the Content-Type parameter:

GM_xmlhttpRequest({
  method: "POST",
  url: myurl,
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  data: mydata,
  onload: function(res) { /* Some code */ }
});

Character set

Various issues can arise when working in the wrong character set. In general, writers are advised to use the standard utf-8 format, which isn't supported in the latest version of Windows Notepad. Other workarounds include using JavaScript to parse characters which would otherwise be invalid in other formats, from integer values.

var values = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33];
alert(String.fromCharCode.apply(String, values));