Troubleshooting (Script Authors): Difference between revisions
m Text replace - "{{Bad samp |1=<pre style="border: none; margin: inherit;">" to "<pre class='sample-bad'>" |
Update for 4.0 |
||
(12 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
If your problem is not listed, try [[Getting Help]]. | |||
If your problem is not listed, | |||
__TOC__ | __TOC__ | ||
== Variables change before a GM_xmlhttpRequest or setTimeout callback runs == | == Variables change before a GM_xmlhttpRequest or setTimeout callback runs == | ||
Line 22: | Line 11: | ||
var link = document.links[i]; | var link = document.links[i]; | ||
GM.xmlHttpRequest({ | |||
method: "GET", | method: "GET", | ||
url: "http://example.com/lookup?url=" + link.href, | url: "http://example.com/lookup?url=" + link.href, | ||
Line 29: | Line 18: | ||
} | } | ||
}); | }); | ||
}</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 49: | Line 38: | ||
for (var i = document.links.length - 1; i >= 0; --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> | </pre> | ||
Solution example with anonymous function: | Solution example with anonymous function: | ||
Line 60: | Line 49: | ||
(function (link_inside) { | (function (link_inside) { | ||
GM_xmlhttpRequest({ | GM_xmlhttpRequest({ | ||
method: "GET", | method: "GET", | ||
Line 67: | Line 55: | ||
link_inside.href = result.responseText; | link_inside.href = result.responseText; | ||
} | } | ||
}); | }); | ||
})(link); | })(link); | ||
} | } | ||
</pre> | </pre> | ||
Solution example with iteration in the "onload" function. Note that this processes each element one by one. | Solution example with iteration in the "onload" function. Note that this processes each element one by one. | ||
Line 77: | Line 64: | ||
<pre class='sample-good'> | <pre class='sample-good'> | ||
(function getNext(i) { | (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); | })(document.links.length - 1); | ||
</pre> | </pre> | ||
== Form POST data == | == Form POST data == | ||
When using [[ | When using [[GM.xmlHttpRequest]] to POST form data, remember to add the '''Content-Type''' parameter: | ||
<pre class='sample-good'> | <pre class='sample-good'> | ||
Line 108: | Line 92: | ||
onload: function(res) { /* Some code */ } | onload: function(res) { /* Some code */ } | ||
}); | }); | ||
</pre> | </pre> | ||
== Character set == | == 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. | 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'> | <pre class='sample-good'> | ||
var values = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]; | var values = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]; | ||
alert(String.fromCharCode.apply(String, values)); | |||
</pre> | |||
</pre> | |||
== | <!-- 4.0 + files = fail | ||
== Installing from /tmp == | |||
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. | |||
--> |
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));