如何通过Chrome扩展程序停用Facebook热键?

fle*_*ale 7 javascript events hotkeys onkeydown google-chrome-extension

我创建了一个Chrome扩展程序,它只使用热键[Alt] + [0 ... 9]来发现facebook使用相同的热键.有没有可能我的扩展可能会禁用Facebook的热键,以便单独开火?我很确定我已经确定了facebook用于实现[Alt] + [0 ... 9]个热键的代码:

document.documentElement.onkeydown=function(a){a=a||window.event;var b=a.target||a.srcElement;var c=a.keyCode==13&&!a.altKey&&!a.ctrlKey&&!a.metaKey&&!a.shiftKey&&CSS.hasClass...
Run Code Online (Sandbox Code Playgroud)

这是在从根文档的头部调用的脚本中.我已尝试以下方法禁用它们:

//contents script:
$().ready( function() {
  document.documentElement.onkeydown = '';
});
Run Code Online (Sandbox Code Playgroud)

乃至

$().ready( function() {
  document.documentElement.onkeydown = function(e){};
});
Run Code Online (Sandbox Code Playgroud)

我进一步猜测这些尝试都不起作用的原因是因为虽然Chrome扩展内容脚本与运行它们的任何网页共享一个DOM,但它们可能不共享编码环境?任何见解将不胜感激!

Rob*_*b W 10

Chrome的内容脚本在沙盒环境中执行[来源].没有直接的方法与global()对象进行通信. window

另一个常见的缺陷是开发人员忘记了脚本注入的方式/时间.

  • 默认情况下,脚本在名为" document_idle " 的点注入.此时,文档不忙(DOMContentLoaded已经解雇,window.onload可能会或可能不会被解雇).
  • 因此,脚本中的函数可能会在声明后立即被覆盖.

要注入一个小脚本,我建议将代码直接添加到内容脚本:

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);
Run Code Online (Sandbox Code Playgroud)

如果要确保不会覆盖该方法,可以使用Object.defineProperty,来定义不可变属性:

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*/
});
Run Code Online (Sandbox Code Playgroud)

Firefox 4+和至少Chrome 5+支持前面提到的方法.如果您还想支持Firefox 2+和Chrome 1+,您可以使用__defineSetter__,以防止onkeydown被定义:

document.documentElement.__defineSetter__('onkeydown', function(){});
Run Code Online (Sandbox Code Playgroud)


Ada*_*res 5

您的直觉是正确的,从内容脚本作为Chrome扩展程序的一部分运行的JavaScript在沙箱中运行,该沙箱无法访问在包含页面中执行的JavaScript.

根据内容脚本上Chrome文档:

However, content scripts have some limitations. They cannot:
*  Use chrome.* APIs (except for parts of chrome.extension)
*  Use variables or functions defined by their extension's pages
*  Use variables or functions defined by web pages or by other content scripts
Run Code Online (Sandbox Code Playgroud)

首先,我建议你考虑不同的快捷键.覆盖您自己的扩展的现有快捷键的功能可能为期望Facebook快捷键的人提供刺耳的用户体验.想象一下,如果扩展覆盖了作为桌面操作系统一部分的ctrl-c和ctrl-p快捷方式进行复制和粘贴 - 我认为你会有一些不安的用户可能会删除改变他们之前学到的行为的东西.

但是,如果您坚持不懈,那么这里是一个解决方法来加载将在包含页面的上下文中执行的JavaScript:

编辑:更新每条评论以引用插件中的JS文件,而不是在网络上托管的文件

首先,您需要在chrome插件中创建一个JavaScript文件:override-fb-hotkeys.js.

首先,您需要在Web上的某个位置托管一个JavaScript文件,其中包含您要在页面中执行的脚本,让我们假设您将其托管在:http://example.com/override-fb-hotkeys.js.

然后,从内容脚本中,您可以将脚本标记插入到引用JavaScript文件的DOM中,如下所示:

    var script = document.createElement('script');
    script.setAttribute("type", "text/javascript");
    script.setAttribute("async", true);
    script.setAttribute("src", chrome.extension.getURL("override-fb-hotkeys.js")); //Assuming your host supports both http and https
    var head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement;
    head.insertBefore(script, head.firstChild)
Run Code Online (Sandbox Code Playgroud)

然后将在包含页面的上下文中提取并执行JavaScript,而不是Chrome插件中的沙盒代码.