Javascripts Works in Bookmarklet but Not Works with Keyboard Maestro

Hi fellows,

I just created a JavaScripts to bypass some webpages' behavior of preventing copy and right click:

javascript: (function() {
    var doc = document;
    var bd = doc.body;
    bd.onselectstart = bd.oncopy = bd.onpaste = bd.onkeydown = bd.oncontextmenu = bd.onmousemove = bd.onselectstart = bd.ondragstart = doc.onselectstart = doc.oncopy = doc.onpaste = doc.onkeydown = doc.oncontextmenu = null;
    doc.onselectstart = doc.oncontextmenu = doc.onmousedown = doc.onkeydown = function() {
        return true;
    };
    with(document.wrappedJSObject || document) {
        onmouseup = null;
        onmousedown = null;
        oncontextmenu = null;
    }
    var arAllElements = document.getElementsByTagName('*');
    for (var i = arAllElements.length - 1; i >= 0; i--) {
        var elmOne = arAllElements[i];
        with(elmOne.wrappedJSObject || elmOne) {
            onmouseup = null;
            onmousedown = null;
        }
    }
    var head = document.getElementsByTagName('head')[0];
    if (head) {
        var style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = "html,*{-moz-user-select:auto!important;}";
        head.appendChild(style);
    }
    void(0);
})();

Then I save this script to Chrome as Bookmark let.

But when I try to execute this code in Keyboard Maestro use Execute Javascript in Google Chrome action, this script become not functional.

How can I solve this?

Have you tested the JavaScript in Chrome in the Chrome Debugger?
Or even just open up Chrome Dev tools to the target page, then trigger your macro with the JavaScript. If there is an error it will show in the Chrome tools.

BTW, this is just a guess, but this may not be possible to use as JavaScript injection due to security protocols in Chrome. Chrome may see the bookmark differently than KM using AppleScript to inject a JavaScript. I ran into this issue trying to get access to data displayed in a window by a Chrome Extension.

Good luck! I hope you find a solution, and if you do please post here.

Hi Michael, thanks for your reply.

I ran this scripts in Chrome Dev Tool Console, and it works fine.

And I simplified it to

javascript: (function() {
    var doc = document;
    var bd = doc.body;
    bd.onselectstart = bd.oncopy = bd.onpaste = bd.onkeydown = bd.oncontextmenu = bd.onmousemove = bd.onselectstart = bd.ondragstart = doc.onselectstart = doc.oncopy = doc.onpaste = doc.onkeydown = doc.oncontextmenu = null;
    doc.onselectstart = doc.oncontextmenu = doc.onmousedown = doc.onkeydown = function() {
        return true;
    };
})();

Still not work with Keyboard Maestro.

Which statement does it first fail on?

Hi Martin,

Since it works within Chrome, so I can not locate the fail when it run with Keyboard Maestro.

It wouldn’t surprise me if the KM log told you nothing. But it’s worth a look.

Also, removing statements and adding them back in, one at a time might help. (Use something like alert() to check progress.) A fairly nasty approach but your code is short.

Can you please provide a URL that you use to test this with.

Hello Michael,

Below is the site which I aimed to handle (Chinese):

And you gotta notice, we can either select nor evoke right-click menu.

Thanks. Do you have an English site I can use for testing?
I'm not comfortable going to a site I can't read and don't know.