Hi.
How I can simulate keypress/keydown in my addon? I tried with obj.trigger(e), KeyboardEvent and everything doesn’t work fine. Why?
I need this addon only for me, so maybe can I switch off this security way in my browser?
Thanks.
Hi.
How I can simulate keypress/keydown in my addon? I tried with obj.trigger(e), KeyboardEvent and everything doesn’t work fine. Why?
I need this addon only for me, so maybe can I switch off this security way in my browser?
Thanks.
Maybe something like:
buttonNode.dispatchEvent(new Event('click'));
@juraj.masiar I tried in that way:
var pressEvent = document.createEvent(“KeyboardEvent”);
pressEvent.initKeyEvent (“keypress”, true, true, null, false, false, false, false, 20, 20);
obj.dispatchEvent(pressEvent);
but it doesn’t work. It is iframe and maybe here is a problem?
Do you see any error messages in any console?
There is a concept of trusted (user-generated) and untrusted (script-generated) events when simulating key presses. Extensions are similarly constrained.
@jscher2000 I don’t see any error in console
try to look into Browser Console - Ctrl + Shift + J. Sometimes errors in content script won’t get printed into Web Console, only into Browser Console.
OK, now I see an error. Look:
TypeError: pressEvent.initKeyboardEvent is not a function
Where is a problem??
Here is my code:
var pressEvent = document.createEvent("KeyboardEvent");
pressEvent.initKeyboardEvent("keypress", true, true, null, false, false, false, false, 75, 0);
initKeyboardEvent
is deprecated, but might work in some circumstances, but try the KeyboardEvent() constructor instead:
Otherwise, “is not a function” or “is not an object” can occur when permissions do not permit access to a thing.
OK, now my code looks that:
var ev = new KeyboardEvent("keypress", {bubbles : true, cancelable : true, key : "Q", char : "Q", shiftKey : true});
obj.dispatchEvent(ev);
But I got:
TypeError: obj.dispatchEvent is not a function
So I think, problem is here:
Otherwise, “is not a function” or “is not an object” can occur when permissions do not permit access to a thing.
So the question is - how I can set this permissions to use this function? I need to use this script only into my browser, so If I can change it manually into my browser - it will be ok, but how I can do it?
I found into net article about conflict jQuery with this method and I need to use:
jQuery.noConflict();
But when I use this code, I can’t use ajax into my code because this code:
$.ajax({
...
});
generate an error. So I tried do it in that way:
var e = jQuery.Event("keypress");
e.which = 75; // # Some key code value
console.log(obj.trigger(e));
And I got into console:
<unavailable>
So I need permissions for it but how I can change this permissions and where I can do it?
If you see <unavailable>
in the console, you’re looking at the wrong console. See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Debugging to determine an appropriate console. For anything happening in a website it’d typically be the web dev tools console.
Edit: also, if you want to dispatch the event to web content, you need to make sure the web content has access to the event. See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#Sharing_content_script_objects_with_page_scripts
I did it and in new console I don’t see anything intresting. In this console is only this:
Could not map contract ID ‘@mozilla.org/fxaccounts/push;1’ to CID {1b7db999-2ecd-4abf-bb95-a726896798ca} because no implementation of the CID is registered. components.manifest:175
Use of nsIFile in content process is deprecated. NetUtil.jsm:307:12
So finally is it possible to use keypress into firefox addon? I’m beginning to doubt