本文介绍了Chrome扩展程序可捕获中间点击并替换URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对chrome扩展程序是完全陌生的,但是我已经阅读了入门指南,并从Google整理了该示例.我想构建一个扩展,使其在活动状态下捕获mydomain.com上的鼠标中键单击事​​件,读取URL,对其进行修改,并使用新创建的URL启动新选项卡.

I'm completely new to chrome extensions but I've read the getting started and assembled the example from Google. I would like to build an extension that while it's active it captures middle mouse click events on mydomain.com, reads the URL , modifies it and launches the new tab with the newly created URL.

据目前为止我所了解的,我需要有一个manifest.json文件和一个my_script.js文件,这些文件将被注入所有mydomain.com页面加载中.这样对吗 ?如果是,我下一步应该怎么做,应该将什么添加到清单和javascript文件中以完成给定的任务.一些代码示例将不胜感激.

From what I understand so far I need to have a manifest.json file and a my_script.js file that will be injected in all mydomain.com page loads. Is this correct ? If yes how should I proceed next and what should I add to my manifest and javascript file to accomplish the given task. Some code examples would be much appreciated.

我还在这里阅读了一对答案 stackoverflow,如果要使用browserAction,则只能在扩展页面中使用,因此不能在内容脚本中使用它.那意味着我将不得不将代码放置在后台页面中,而不是my_script.js.请告知我该如何进行.

I've also read a couple of answers here on stackoverflow and if browserAction is going to be used it can only be used in extension pages, so you can not use it in content scripts. That would mean I would have to place my code in the background page instead of my_script.js . Please advice how should I proceed.

谢谢

工作脚本解决方案是:

$(document).click(function(e) {
    var urlToGo = window.location.href;

    // middle button
    if (e.which == 2) {
        urlToGo = "http://google.com"; 
        window.open(urlToGo);       
        e.preventDefault();
        e.stopPropagation();
    }
});

推荐答案

您可以从我的简单Chrome扩展程序内容脚本框架开始 https://github.com/robin-drexler/Simple-Chrome-Extension-Content-Script-Skeleton ,它提供清单和内容脚本,将会在您访问的每个页面上执行.

You can start with my simple Chrome Extension Content Script Skeleton https://github.com/robin-drexler/Simple-Chrome-Extension-Content-Script-Skeleton, which provides a manifest and a content script, that'll be executed on every page you visit.

现在,您可以继续并实现所需的功能.然后,您可以使用window.open(更简便的方式)打开新的标签页/窗口,也可以使用本机的Chrome API来打开新的标签页.

Now you can go on and implement your desired feature.Then you could either usewindow.open to open a new tab/window (easier way) or the native Chrome APIs to open a new tab.

window.open(在内容脚本中)

window.open (in content script)

 $(function() {
        $(document).on('click', function(e){
            //do some replace magic here
            var url = 'http://google.com';
            if (e.which === 2) {
                window.open(url);       
            }        
        });
    });

http://jsfiddle.net/886jY/2/

Chrome API的有趣读物

Interesting reads for Chrome APIs

后台页面和内容脚本之间的消息传递. IIRC,您只能在后台页面中使用CHrome Tab API. http://developer.chrome.com/extensions/messaging.html

Messaging between background page and content script. IIRC you can only use the CHrome Tab APIs in the background page.http://developer.chrome.com/extensions/messaging.html

Chrome Tab API http://developer.chrome.com/extensions/tabs.html

Chrome Tab APIhttp://developer.chrome.com/extensions/tabs.html

这篇关于Chrome扩展程序可捕获中间点击并替换URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 05:51