Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

greasemonkey: addEventListener questions

I've a userscript as the following:

document.addEventListener('click', alert('hello monkey'), true);

There were two problems:

  1. "hello monkey" is alerted only when refreshing the browser, not work when clicking window.

  2. using GM's 'manage user script' to edit the script, the change doesn't happen. (The source code on the local disc was changed.)

like image 758
Paul Avatar asked Jul 15 '10 23:07

Paul


People also ask

What is the purpose of addEventListener?

The addEventListener() method makes it easier to control how the event reacts to bubbling. When using the addEventListener() method, the JavaScript is separated from the HTML markup, for better readability and allows you to add event listeners even when you do not control the HTML markup.

Does addEventListener return value?

The event handler function won't run until the event happens. By that time, the function that called addEventHandler will have finished running and returned. The event handler function needs to either deal with the data itself, or call other functions to do it. The data has to travel forwards, it can't go back.

Is addEventListener a DOM API?

addEventListener is a DOM Level 2 (2001) feature.


1 Answers

You need to bind it so it isn't executed automatically...

document.addEventListener('click', function(){alert('hello monkey')}, true);

Not sure about #2 though.

like image 68
meder omuraliev Avatar answered Sep 18 '22 17:09

meder omuraliev