如何使用 Chrome 扩展程序禁用 Facebook 热键? [英] How to disable facebook hotkeys with Chrome extension?

查看:22
本文介绍了如何使用 Chrome 扩展程序禁用 Facebook 热键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 Chrome 扩展,它使用热键 [Alt]+[0...9] 只是为了发现 facebook 使用相同的热键.有什么办法可以让我的扩展程序禁用 facebook 的热键,这样我的火就可以了?我相当确定我已经确定了 facebook 用来实现他们的 [Alt]+[0...9] 热键的代码:

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 facebook's hotkeys so that mine fire alone? I'm fairly certain I have identified the code facebook uses to implement their [Alt]+[0...9] hotkeys:

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...

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

This is in a script called from the head of the root document. I have tried the following to disable them:

//contents script:
$().ready( function() {
  document.documentElement.onkeydown = '';
});

甚至

$().ready( function() {
  document.documentElement.onkeydown = function(e){};
});

我进一步猜测,这两种尝试都不奏效的原因是,尽管 Chrome 扩展内容脚本与其运行的任何网页共享一个 DOM,但也许它们不共享编码环境?任何见解将不胜感激!

I am guessing further that the reason neither of these attempts work is because although Chrome extension content scripts share a DOM with any webpage on which they run, perhaps they do not share coding environments? Any insight would be appreciated!

推荐答案

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

Your intuition is correct, the JavaScript that runs from a content script as part of a Chrome Extension is run in a sandbox that does not have access to the JavaScript that is executed in the containing page.

根据 关于内容脚本的 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

首先,我建议您考虑使用不同的快捷键.为您自己的扩展覆盖现有快捷键的功能可能会给期待 Facebook 快捷键的人带来不和谐的用户体验.想象一下,如果扩展覆盖了 ctrl-c 和 ctrl-p 快捷方式,这些快捷方式是桌面操作系统的一部分,用于复制和粘贴 - 我认为您会有一些心烦意乱的用户,他们可能会删除改变他们之前学到的行为的东西.

First off, I would recommend that you consider different shortcut keys. Overriding the functionality of existing shortcut keys for your own extension could provide a jarring user experience for someone that is expecting the Facebook shortcut key. Imagine if an extension overrode the ctrl-c and ctrl-p shortcuts that are a part of the desktop OS for copy and paste - I think you would have some upset users that would probably remove the thing that changed the behavior they learned prior.

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

However, if you are insistent, then here is a workaround to loading JavaScript that will execute in the context of the containing page:

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

Updated per comment to reference JS file in a plugin instead of one hosted on the web

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

First, you will need to create a JavaScript file in your chrome plugin: override-fb-hotkeys.js.

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

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

Then, from your content script, you can insert a script tag into the DOM that references your JavaScript file, something like this:

    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)

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

The JavaScript will then be fetched and executed in the context of the containing page, not the sandboxed code from the Chrome plugin.

这篇关于如何使用 Chrome 扩展程序禁用 Facebook 热键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆