Editing
Troubleshooting (Script Authors)
(section)
From Greasespot Wiki
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Variables change before a GM_xmlhttpRequest or setTimeout callback runs == Problem example: <pre class='sample-bad'> 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; } }); }</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). 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: <pre class='sample-good'> 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); } </pre> Solution example with anonymous function: <pre class='sample-good'> 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); } </pre> Solution example with iteration in the "onload" function. Note that this processes each element one by one. <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>
Summary:
Please note that all contributions to Greasespot Wiki are considered to be released under the GNU Free Documentation License 1.3 or later (see
Greasespot Wiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Page actions
Page
Discussion
Read
Edit
History
Page actions
Page
Discussion
More
Tools
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main Page
Recent changes
Random page
Search
Tools
What links here
Related changes
Special pages
Page information