Help with fake event

Subscribe to Help with fake event 5 posts, 4 voices

 
Mindeye Scriptwright

One of the functions of my script YousableTubeFix (http://userscripts.org/scripts/show/13333) is to automatically expand video description in a Youtube page. A "(more)" link button with an associated onclick event handler does that, so I usually did something like this:


var expandLink = $x1("//div[@class='videoDescDiv collapse-content']//a[@class='smallText eLink'][@onclick]");
if (expandLink) {
// Executes the onclick function
expandLink.wrappedJSObject.onclick(); // unsafeWindow context
}

$x1 is a function that executes a Xpath search and return the first item found

Recently I changed the script to create a fake event and dispatch it. It has the additional advantage of don't breaking the sandbox:


var expandLink = $x1("//div[@class='videoDescDiv collapse-content']//a[@class='smallText eLink'][@onclick]");
if (expandLink) {
var clickEvt = document.createEvent("HTMLEvents");
clickEvt.initEvent("click", true, true);
expandLink.dispatchEvent(clickEvt);
}

It worked until recently, I think because Youtube changed something. I rolled back the change in my script and it is now working with the old code, but I'd like what is the problem with the event code

Thanks in advance

 
Mikado Scriptwright

You're creating event of wrong type. Check this example.
I wonder why your code even worked (if it did), since "click" doesn't belong to "HTMLEvents".

PS: maybe location.assign('javascript:void (function(){' + expandLink.getAttribute('onclick') + '})()'); would be a simple alternative?

 
Descriptor Scriptwright

This is a pretty good site: http://www.howtocreate.co.uk/tutorials/javascri...
Though there's a lot of cross-browser stuff and sometimes he skips over the better methods (because they don't work in IE), and sometimes it is hard to figure out which browser he is coding for.
Still I learned a lot from that, and I refer back to it.

 
LouCypher Scriptwright

Descriptor wrote:

and sometimes it is hard to figure out which browser he is coding for.

Opera, AFAIK. I used to be there too.

 
Mindeye Scriptwright

Thanks Descriptor and LouCypher for the documentation, I'll check it

Mikado:

You're creating event of wrong type. Check this example.
I wonder why your code even worked (if it did), since "click" doesn't belong to "HTMLEvents".

I can assure it worked before. I had also checked the documentation in Mozilla Developer site, but I haven't checked the official documentation since I was following the Greasemonkey wiki example (http://wiki.greasespot.net/Location_hack#Really...) and it uses HTMLEvents

The location hack is good, but I'd rather use initMouseEvent since it respects possible event Listeners added with addEventListener, and it bubbles up like a real event