That's because you're invoking the function instead of providing a pointer to it.
imgel.linkType = 'img';
imgel.addEventListener('click', sSend, false);
.........
audio.linkType = 'audio';
audio.addEventListener('click', sSend, false);
.........
function sSend() {
GM_xmlhttpRequest({
method: "GET",
url: "http://localhost/index.php?url='" + this.linkType + ", " + document.URL + ", " + document.getElementsByTagName("title")[0].innerHTML + "'",
onload: function(response) {
}
});
}
Alternatively you can use bind
which creates a new anonymous function wrapper passing its first parameter as this
inside the function:
imgel.addEventListener('click', sSend.bind('img'), false);
.........
audio.addEventListener('click', sSend.bind('audio'), false);
.........
function sSend(event) {
GM_xmlhttpRequest({
method: "GET",
url: "http://localhost/index.php?url='" + this + ", " + document.URL + ", " + document.getElementsByTagName("title")[0].innerHTML + "'",
onload: function(response) {
}
});
}
I have a problem with an old script that try to fix myself (i try....).
Like usual , i am not coder, but maybe it is related to the same problem (click event):
Can you drop an eye on it ?
Here my post in Greasmonkey Google group :
Script broken after last Greasemonkey version 2.2 Update (no action possible on links)
It seems partially solved with Greasmonkey v.3.2beta2, but partially ...
That's because you're invoking the function instead of providing a pointer to it.The easiest way to fix the error:
imgel.addEventListener('click', function(){sSend("img")}, false);
The easiest way to fix the error
Doesn't seem easier to me comparing to the two methods I mentioned. Just another method.
Onclick event triggered with out click
Having trouble working out why the onclick event is executing sSend() as soon as a page has loaded not on an actually click. If I set the onclick to sSend without parameter it works fine? I've tried also using addEventListener but dosen't seem to matter.
// ==UserScript==
// @name Like
// @namespace none.com
// @description Like page
// @include *
// @version 1
// @grant GM_xmlhttpRequest
// ==/UserScript==
cImg();
function cImg() {
var divContainer = document.createElement('div');
divContainer.style = 'position:absolute;top: 0px;right: 0px;';
var imgel = document.createElement('img');
imgel.src = 'img.png';
imgel.style = 'width: 25px; height: 25px;';
imgel.addEventListener('click', sSend("img"), false);
var audio = document.createElement('img');
audio.src = 'img2.png';
audio.style = 'width: 25px; height: 25px;';
//audio.onclick = sSend("audio");
divContainer.appendChild(imgel);
divContainer.appendChild(audio);
document.body.appendChild(divContainer);
}
function aalert(mes){
alert(mes);
}
function sSend(linkType){
GM_xmlhttpRequest({
method: "GET",
url: "http://localhost/index.php?url='" + linkType + ", " + document.URL + ", " + document.getElementsByTagName("title")[0].innerHTML + "'",
onload: function(response) {
}
});
}