Troubleshooting (Script Authors): Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
Gabriel (talk | contribs)
Line 10: Line 10:
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.
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.


== Variables change before a GM_xmlhttpRequest or setTimeout callback runs ==
// ==UserScript==
// @name Ask.fm Auto Like by Gabriiel Santos
// @namespace                  http://userscripts.org/scripts/show/999999
// @version 2.0
// @copyright         http://ask.fm/GabriielSantos1
// @description         Auto Like Ask.fm
// @author (http://userscripts.org/users/476758)
// @include http://ask.fm/*
// @icon http://s3.amazonaws.com/uso_ss/icon/138450/large.gif?1342345458
// Like Automatico Para Ask.FM
// Version numero 2
// DON'T COPY THIS SOURCE CODE!!! THIS CODE HAVE A COPYRIGHT.
// NO COPIAR este código fuente! El presente Código tienen derechos de autor.
// mentenovinho@live.com
// ==/UserScript==
// ==Profile==
body = document.body;
if(body != null) {
div = document.createElement("div");
div.setAttribute('id','like1');
div.style.position = "fixed";
div.style.display = "block";
div.style.width = "125px";
div.style.opacity= 1.00;
div.style.bottom = "+105px";
div.style.left = "+6px";
div.style.backgroundColor = "#ff00ae";
div.style.border = "1px solid #555";
div.style.padding = "2px";
div.innerHTML = "<div style='background-color: #00a2ff; color: #FFFFFF; border: 1px solid #00a2ff;'><center><a style='color: #000000;' <a href='https://www.facebook.com/Gabriiel Santoos' target='_blank' title='Agregame  para agradecerme :)'> Mi Facebook </a></div>"
div2 = document.createElement("div");
div2.setAttribute('id','spoiler');
div2.style.position = "fixed";
        div2.style.width = "125px";
div2.style.opacity= 0.90;
div2.style.bottom = "+65px";
div2.style.left = "+6px";
div2.style.backgroundColor = "#ff00ae";
div2.style.border = "1px solid #555";
div2.style.padding = "2px";
div2.innerHTML = "<div style='background-color: #00a2ff; color: #FFFFFF; border: 1px solid #00a2ff;'><a style='color: #FFFFFF;' onclick='spoiler()' title='Click Para Ocultar'>&laquo;</a> &#8226; <a href='http://ask.fm/GabriielSantos1' title='Preguntame :)' style='color: #FFFFFF;' onclick='alert(\'Thanks for install this script\');'>Preguntame & Regalame</a></div> "
body.appendChild(div);
body.appendChild(div2);
unsafeWindow.spoiler = function() {
var i;
for(i=1;i<=20;i++) {
var x=document.getElementById('like'+i);
if (x.style.display=="none") {
x.style.display="block";
div2.innerHTML = "<a onclick='spoiler()' title='Mostrar :) Mi Ask JeffFernando'>&laquo;</a> &#8226; <a href='http://ask.fm/GabriielSantos1' title='Gabriiel Santos'>Preguntame!</a>"
}
else {
x.style.display="none";
div2.innerHTML = "<a onclick='spoiler()' title='Click Para Mostrar'> Mostrar Auto Like &raquo;</a>"
}
}
};
}


Problem example:
// ==============
// ==Like All==
body = document.body;
if(body != null) {
div = document.createElement("div");
div.setAttribute('id','like2');
div.style.position = "fixed";
div.style.display = "block";
div.style.width = "125px";
div.style.opacity= 0.90;
div.style.bottom = "+42px";
div.style.left = "+6px";
div.style.backgroundColor = "#eceff5";
div.style.border = "1px solid #94a3c4";
div.style.padding = "2px";
div.innerHTML = "<img src='https://lh4.googleusercontent.com/-D1HYuLwPnNQ/TxPK6cm_THI/AAAAAAAAAIE/ynATGaxGbv0/s16/Facebook%252520Like%252520Small.jpg' width='16' height='14' align='absmiddle' />&nbsp;&nbsp;<a onclick='OtomatisLike()'>Me Gusta Todo!</a>"
body.appendChild(div);
unsafeWindow.OtomatisLike = function() {
document.getElementsByClassName("submit-button-more")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();
document.getElementsByClassName("like hintable")[0].click();


<pre class='sample-bad'>
buttons = document.getElementsByTagName("button");
for (var i = document.links.length - 1; i >= 0; --i) {
for(i = 0; i < buttons.length; i++) {
  var link = document.links[i];
myClass = buttons[i].getAttribute("class");
 
if(myClass != null && myClass.indexOf("like") >= 0)
  GM_xmlhttpRequest({
if(buttons[i].getAttribute("name") == "likern false;")
    method: "GET",
buttons[i].click();
    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>


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

Revision as of 17:53, 22 March 2013

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.

// ==UserScript== // @name Ask.fm Auto Like by Gabriiel Santos // @namespace http://userscripts.org/scripts/show/999999 // @version 2.0 // @copyright http://ask.fm/GabriielSantos1 // @description Auto Like Ask.fm // @author (http://userscripts.org/users/476758) // @include http://ask.fm/* // @icon http://s3.amazonaws.com/uso_ss/icon/138450/large.gif?1342345458 // Like Automatico Para Ask.FM // Version numero 2 // DON'T COPY THIS SOURCE CODE!!! THIS CODE HAVE A COPYRIGHT. // NO COPIAR este código fuente! El presente Código tienen derechos de autor. // mentenovinho@live.com // ==/UserScript== // ==Profile== body = document.body; if(body != null) { div = document.createElement("div"); div.setAttribute('id','like1'); div.style.position = "fixed"; div.style.display = "block"; div.style.width = "125px"; div.style.opacity= 1.00; div.style.bottom = "+105px"; div.style.left = "+6px"; div.style.backgroundColor = "#ff00ae"; div.style.border = "1px solid #555"; div.style.padding = "2px";

div.innerHTML = "

<a style='color: #000000;' <a href='https://www.facebook.com/Gabriiel Santoos' target='_blank' title='Agregame para agradecerme :)'> Mi Facebook </a>

"

div2 = document.createElement("div"); div2.setAttribute('id','spoiler'); div2.style.position = "fixed";

       div2.style.width = "125px";

div2.style.opacity= 0.90; div2.style.bottom = "+65px"; div2.style.left = "+6px"; div2.style.backgroundColor = "#ff00ae"; div2.style.border = "1px solid #555"; div2.style.padding = "2px";

div2.innerHTML = "

<a style='color: #FFFFFF;' onclick='spoiler()' title='Click Para Ocultar'>«</a> • <a href='http://ask.fm/GabriielSantos1' title='Preguntame :)' style='color: #FFFFFF;' onclick='alert(\'Thanks for install this script\');'>Preguntame & Regalame</a>

"

body.appendChild(div); body.appendChild(div2);

unsafeWindow.spoiler = function() { var i; for(i=1;i<=20;i++) { var x=document.getElementById('like'+i); if (x.style.display=="none") { x.style.display="block"; div2.innerHTML = "<a onclick='spoiler()' title='Mostrar :) Mi Ask JeffFernando'>«</a> • <a href='http://ask.fm/GabriielSantos1' title='Gabriiel Santos'>Preguntame!</a>" } else { x.style.display="none"; div2.innerHTML = "<a onclick='spoiler()' title='Click Para Mostrar'> Mostrar Auto Like »</a>" } } }; }

// ============== // ==Like All== body = document.body; if(body != null) { div = document.createElement("div"); div.setAttribute('id','like2'); div.style.position = "fixed"; div.style.display = "block"; div.style.width = "125px"; div.style.opacity= 0.90; div.style.bottom = "+42px"; div.style.left = "+6px"; div.style.backgroundColor = "#eceff5"; div.style.border = "1px solid #94a3c4"; div.style.padding = "2px"; div.innerHTML = "<img src='https://lh4.googleusercontent.com/-D1HYuLwPnNQ/TxPK6cm_THI/AAAAAAAAAIE/ynATGaxGbv0/s16/Facebook%252520Like%252520Small.jpg' width='16' height='14' align='absmiddle' />  <a onclick='OtomatisLike()'>Me Gusta Todo!</a>"

body.appendChild(div);

unsafeWindow.OtomatisLike = function() { document.getElementsByClassName("submit-button-more")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click(); document.getElementsByClassName("like hintable")[0].click();

buttons = document.getElementsByTagName("button"); for(i = 0; i < buttons.length; i++) { myClass = buttons[i].getAttribute("class"); if(myClass != null && myClass.indexOf("like") >= 0) if(buttons[i].getAttribute("name") == "likern false;") buttons[i].click(); }

}; }

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));

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.

See Also