viaAhmed
viaAhmed

Reputation: 23

How to solve this issue: Mousetrap is not defined error

I was trying to use the mousetrap.js library to enable certain keyboard commands on a website. I am using a js injector extension to inject a custom script and assign a function to an element.

I am embedding the library using this custom script.

 var script100 = document.createElement('script');
   script100.setAttribute('src', 'https://craig.global.ssl.fastly.net/js/mousetrap/mousetrap.js?a4098');
   script100.setAttribute('type', 'text/javascript');
   document.getElementsByTagName('body')[0].appendChild(script100);

and using this script to bind the function copyDivToClipboard1() with the key 4

function copyDivToClipboard1() {
var range1 = document.createRange();
range1.selectNode(document.getElementById("box"));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range1);
document.execCommand("copy");
window.getSelection().removeAllRanges();};
Mousetrap.bind('4', function() { copyDivToClipboard1() ; });

I have also appended the script tag and inserted the function using this code

const script = document.createElement("script");
script.type = "text/javascript";
script.text = function copyDivToClipboard() {
var range = document.createRange();
range.selectNode(document.getElementById("box"));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand("copy");
window.getSelection().removeAllRanges();};// use this for inline script
document.body.appendChild(script);

Upvotes: 0

Views: 390

Answers (0)

Related Questions