How to disable facebook hotkeys with Chrome extension?

前端 未结 3 1061
南旧
南旧 2020-12-08 12:45

I have created a Chrome extension that uses the hotkeys [Alt]+[0...9] only to discover facebook uses the same hotkeys. Is there any way possible my extension could disable f

3条回答
  •  臣服心动
    2020-12-08 12:55

    Chrome's Content scripts are executed in a Sandboxed environment [source]. There is no direct way to communicate with the global (window) object.

    Another common pitfall is that the developer forgets how/when the script is injected.

    • By default, the script is injected at a point called "document_idle". At this point, the document is not busy (DOMContentLoaded has fired, window.onload may or may not have fired).
    • As a result, the functions in the script may be overwritten immediately after declaration.

    To inject a small script, I recommend to add the code directly to the Content Script:

    var actualCode = '/* Code here (see below for inspiration) */';
    
    var script = document.createElement('script');
    script.appendChild(document.createTextNode(actualCode));
    (document.head || document.documentElement).appendChild(script);
    script.parentNode.removeChild(script);
    

    If you want to make sure that the method is not going to be overwritten, you can use Object.defineProperty, to define an immutable property:

    Object.defineProperty(document.documentElement, 'onkeydown', {
        value: function() {},
        writable: false,     /* Cannot be overwritten, default false */
        configurable: false, /* Cannot be deleted, or modified */
        enumerable: true     /* Does not really matter. If true, it's visible in
                                 a for-loop. If false, it's not*/
    });
    

    The previously mentioned method is supported in Firefox 4+ and at least Chrome 5+. If you want to also support Firefox 2+ and Chrome 1+, you can play with the __defineSetter__, to prevent onkeydown from being defined:

    document.documentElement.__defineSetter__('onkeydown', function(){});
    

提交回复
热议问题