I'm trying to create a Chrome Extension which allows me to press a key on my keyboard, and map it to a HTML button on the screen. I was just trying to get the click to work, before even bothering mapping it to a key, and I can't. When I try and run it, the console outputs this: Uncaught TypeError: Cannot read property 'dispatchEvent' of null
.
Any help would be much appreciated. Here is my code:
const selector = 'button.btn-standard.call-to-action';
const button = document.querySelector(selector);
const triggerClick = (target) => {
let event = document.createEvent ('MouseEvents');
event.initEvent ('mousedown', true, true);
target.dispatchEvent(event);
event = document.createEvent ('MouseEvents');
event.initEvent ('mouseup', true, true);
target.dispatchEvent(event);
};
triggerClick(button);
HTML of the button <button class="btn-standard call-to-action">Search</button>
Update: i have it working in the console but when i try and use it through the chrome extension that is when i get that error. i believe its because chrome extensions cant execute JS thru the popup.HTMl so i put it in the background.js but i still cant seem to get it working