Search code examples
javascriptjquerygoogle-chrome-extensionanonymous-function

Need help calling anonymous function/plugin through chrome extension


I am developing a chrome extension to automate parts of a website. I need to trigger the click event of a link. It has an event handler through an anonymous function like this...

(function($, dialog) {
	function toggle(event) {
	}
	
	function addEventListeners() {
		form.on('click', '.toggle', toggle);
	}

	function init() {
	}

	$(init);
}(jQuery, window.silk.classes.dialog));

Traditional methods such as $('#elm').click(), trigger(), triggerHandler(), etc. do not work. I guess I need to raise the toggle() function within the anonymous plugin.

I can figure out how to trigger it through the extension, if someone can help me figure out how to trigger the handler through the console.

Appreciate your inputs.


Solution

  • I managed to figure it out. At the end of the day, I was trying to trigger a handler within an anonymous function. The following lines of code served to that end.

            var btn = document.getElementById('toggle');
            btn.addEventListener('click', function() {
                this.removeEventListener('click', arguments.callee, false);
                }, false);
    
            var event = document.createEvent('Event');
            event.initEvent('click', true, true);
            btn.dispatchEvent(event);