javascript - Keydown Which Not Working Chrome Extension -


i've been struggling idea of google extension, , last hope of mine ! :))

well, want click button on chrome extension cause keydown simulation on page extension running.

i think chrome has safety issues on idea, blocks keyboard simulation (makes event istrusted:false) , deletes property.

the function wrote works fine on jsfiddle , appears chrome extension in different manner.

here content script file :

chrome.runtime.onmessage.addlistener(function (request, sender, sendresponse) {   if(request.action == "scrolltotop"){    }   else if(request.action == "scrolltobottom"){    }   else if(request.action == "enter"){     triggerkeyboardevent(document,13);   }    function triggerkeyboardevent(el, keycode){     var event = new event("keydown", {"bubbles":true, "cancelable":true});     event.which = keycode;     el.dispatchevent(event);   }  }); chrome.runtime.sendmessage({action : "show"}); 

the log on jsfiddle writes:

event {istrusted: false, which: 13} 

the log on website:

document.addeventlistener('keydown',function (e) {       console.log(e) } 

writes just:

event {istrusted: false} 

thanks @bg101 , @rob w found out solution script injection!

the thing according mdn keyboardevent.initkeyboardevent() depricated, replaced code with:

var event = new event(event, {"bubbles":true, "cancelable":true}); 

also, wanted trigger run on document, removed element selector things. here got:

chrome.runtime.onmessage.addlistener(function (request, sender, sendresponse) {   if(request.action == "scrolltotop"){     triggerkeyboardeventondocument("keydown",38);    }   else if(request.action == "scrolltobottom"){     triggerkeyboardeventondocument("keydown",40);    }   else if(request.action == "enter"){     triggerkeyboardeventondocument("keydown",13);   }    function triggerkeyboardeventondocument(event, keycode){     var script = document.createelement('script');      script.textcontent = '(' + function(event, charcode) {          //create event         var event = new event(event, {"bubbles":true, "cancelable":true});          // define custom values         // part requires script run in page's context         var gettercode = {get: function() {return charcode}};         var getterchar = {get: function() {return string.fromcharcode(charcode)}};         object.defineproperties(event, {           charcode: gettercode,           which: gettercode,           keycode: gettercode, // not correct           key: getterchar,     // not correct           char: getterchar         });         document.dispatchevent(event);       } + ')(' + '\"' + event + '\", '+ keycode + ')';      (document.head||document.documentelement).appendchild(script);     script.parentnode.removechild(script);   }  }); chrome.runtime.sendmessage({action : "show"}); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -