1

私は拡張機能を作って遊んでいます。ユーザーが拡張機能をインストールしている場合、ユーザーがWebページでクリックしたリンクをキャプチャしたいと思います。これを行う方法はよくわかりませんが、簡単なようです。追加するかもしれませんが、プラグインがインストールされて有効になっている限り、これを実行したいのですが、ユーザーがツールバーで「アクティブ化」するために何かをする必要はありません。

開始方法がわからない。また、JSファイルが多すぎると思いますが、そのうちの1つをコンソールに記録しようとしています。どちらもしません。私の最終的な目標は、特定の場所に移動した場合に、それらをイントラネットページにリダイレクトしたいということです。

background.js

var redirectedSites = ["https://www.facebook.com/profile.php?id=<SOMEPROFILEID>"];
// when the browser tries to get to a page, check it against a list
chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        console.log('is this even getting hit?');
        for(var i=0; i < redirectedSites.length; ++i) {
            // if the attempt is to a listed site, redirect the request
            if( details.url == redirectedSites[i] )
               return {redirectUrl: "http://intranet/landing?from=" + details.url };
         }
    },
    {urls: ["*://www.facebook.com/*"]},
    ["blocking"]
);

マニフェスト.json

{
 "name": "Capture Click",
 "version": "0.1",
 "description": "Simple tool that logs clicked links.",
 "permissions": [
"tabs",
"webRequest",
"webRequestBlocking",
    "https://*.facebook.com/*"
 ],
"background": {
   "scripts": ["background.js"]
 },
 "manifest_version": 2
}
4

1 に答える 1

2

I've given some advice in the comments, but the best way to solve your actual larger problem is with a webRequest handler:

var redirectedSites = ["http://www.google.com/foobar", ...];
// when the browser tries to get to a page, check it against a list
chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        for(var i=0; i < redirectedSites.length; ++i) {
            // if the attempt is to a listed site, redirect the request
            if( details.url == redirectedSites[i] )
                return {redirectUrl: "http://intranet/landing?from=" + details.url };
        }
    },
    {urls: ["*://www.google.com/*"]},
    ["blocking"]);

This is a really simple example, but I hope you get the idea. Here, details.url is the page the user is trying to get to, and the returned object has a redirectUrl property that redirects the attempt to visit the page. My example checks details.url against a list of target sites; you could use a regex or something else that's more robust.

Note that this will affect not only clicked links and typed-in URLs, but also resources (scrips, images) and Ajax requests.

于 2012-04-24T02:31:56.810 に答える