如何使用Chrome扩展功能禁用facebook热键? [英] How to disable facebook hotkeys with Chrome extension?
问题描述
我已经创建了一个使用热键[Alt] + [0 ... 9]的Chrome扩展程序,只能发现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 ...
这是在根文件头部调用的脚本。我尝试过以下操作来禁用它们:
//内容脚本:
$() ){
document.documentElement.onkeydown ='';
});
甚至
code> $()。ready(function(){
document.documentElement.onkeydown = function(e){};
});
我进一步猜测,这两个尝试都不能正常工作的原因是因为虽然Chrome扩展内容脚本共享一个DOM与他们运行的任何网页,也许他们不共享编码环境?任何见解将不胜感激!
您的直觉是正确的,JavaScript内容脚本作为Chrome扩展程序的一部分运行在一个沙箱中运行,无法访问在包含页面中执行的JavaScript。
但是,内容脚本有一些限制。他们不能:
*使用chrome。* API(chrome.extension的部分除外)
*使用由扩展名的页面定义的变量或函数
*使用由网页定义的变量或函数,由其他内容脚本
首先,我建议您考虑不同的快捷键。覆盖您自己的扩展的现有快捷键的功能可以为期待Facebook快捷键的人提供一个刺激的用户体验。想象一下,如果一个扩展覆盖了ctrl-c和ctrl-p快捷方式,这是桌面操作系统的一部分进行复制和粘贴 - 我想你会有一些不高兴的用户,可能会删除改变他们之前学到的行为的东西。 / p>
但是,如果您是坚持的,那么这里是加载将在包含页面的上下文中执行的JavaScript的解决方法:
编辑:更新每个注释以引用插件中的JS文件,而不是在网络上托管的文件
首先,您将需要在您的Chrome插件中创建一个JavaScript文件: override-fb-hotkeys.js
。
,您将需要在Web上的某个地方托管一个JavaScript文件,其中包含您要在页面中执行的脚本,可以说如下: http://mysite.com/override-fb -hotkeys.js
。
然后,从内容脚本中,您可以将脚本标签插入到引用的DOM中您的JavaScript文件,如下所示:
var script = document.createElement('script');
script.setAttribute(type,text / javascript);
script.setAttribute(async,true);
script.setAttribute(src,chrome.extension.getURL(override-fb-hotkeys.js)); //假设你的主机支持http和https
var head = document.head || document.getElementsByTagName(head)[0] || document.documentElement中;
head.insertBefore(script,head.firstChild)
然后将JavaScript抓取,在包含页面的上下文中执行,而不是来自Chrome插件的沙盒代码。
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 = '';
});
and even
$().ready( function() {
document.documentElement.onkeydown = function(e){};
});
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!
You 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.
Per the Chrome doc on Content Scripts:
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
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 short cut 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.
However, if you are insistent, then here is a workaround to loading JavaScript that will execute in the context of the containing page:
Edit: Updated per comment to reference JS file in plugin instead of one hosted on the web
First, you will need to create a JavaScript file in your chrome plugin: override-fb-hotkeys.js
.
First, you will need to host a JavaScript file somewhere on the web that contains the script that you want to execute in the page, lets say you host it at: http://mysite.com/override-fb-hotkeys.js
.
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)
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屋!