I am creating a Firefox Extension to change keyboard functions in the game https://agar.io/. The script.js
below works fine if pasted in the console (CTRL+Shift+K) or loaded in scratchpad (Shift+F4) (scratchpad doesn't exists anymore in Firefox 72.0.2). But if I load my extension in Load Temporary Add-on in the about:debugging
page, the functions
window.onkeydown({keyCode: 87});
window.onkeyup({keyCode: 87});
core.setMinimap(minimap); // This is a function of the game and works in the console for minimap=true.
don't work, as you can see by the logs test-points-finished
that don't appear in the console when I press KEY_O
or KEY_M
.
So, why the functions above don't work inside the extension? Please explain me because I'm new to JavaScript.
script.js
:
(function() {
var minimap = false;
var overwriting = function(evt) {
if (evt.keyCode === 79) { // If reads KEY_O, then simulate press and release of KEY_W
console.log("test-point-1-started");
window.onkeydown({keyCode: 87}); // Simulate press of KEY_W
console.log("test-point-1-finished");
window.onkeyup({keyCode: 87}); // Simulate release of KEY_W
}
else if (evt.keyCode === 77) {// Toggle minimap with Key_M
minimap=!minimap;
console.log("test-point-2-started");
core.setMinimap(minimap);
console.log("test-point-2-finished");
}
}
window.addEventListener('keydown', overwriting);
})();
Here is the manifest.json
:
{
"manifest_version": 2,
"name": "My firefox extension",
"version": "1.0",
"description": "Test extension.",
"content_scripts": [
{
"matches": ["*://*.agar.io/*"],
"js": ["script.js"]
}
]
}
Thanks.