我可以推动"返回"用javascript打开铬的按钮

时间:2015-07-11 14:46:09

标签: javascript google-chrome google-chrome-extension

如果我尝试加入m.facebook.com,我想帮助了解如何转到Chrome的上一页

让我告诉你我的代码:

var host = "http://www.google.com";          // here you put the URL that you want to be redirected 



chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
         return {redirectUrl: host + details.url.match(/^https?:\/\/[^\/]+([\S\s]*)/)[1]};              //where it says "host" you put the name of the var that you have set above to the URL that you want to be redirected


    },
    {
        urls: [
            "*://m.facebook.com/*"                  // here you put the URL that you want to block.
        ],
        types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
    },
    ["blocking"]
);

清单

{
    "name": "Facebook Notification Block",
    "description": "Block Notification",
    "version": "1.0",
    "manifest_version": 2,
    "background": {
        "scripts": [
            "background.js"
        ]
    },
    "permissions": [
            "webRequest",
            "*://facebook.com/*",
            "*://m.facebook.com/*",
            "*://www.facebook.com/*",
        "webRequestBlocking"
    ]
}

使用此代码我可以"阻止" m.facebook.com并重定向到" www.google.com"我已经安装了扩展程序,从我的Chrome浏览器。但我只想转到上一页。

UPDATE1:

像克里斯托弗建议我尝试这些代码

添加到清单权限"历史"

我做了像这样的background.js

var goBack = function() {
    window.history.back();
}

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
         return {goBack};               


    },
    {
        urls: [
            "*://m.facebook.com/*"                  // here you put the URL that you want to block.
        ],
        types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
    },
    ["blocking"]
);

但我无法弄清楚我的错误。

2 个答案:

答案 0 :(得分:2)

var goBack = function() {
    window.history.back();
}

这是你的意思吗?

答案 1 :(得分:0)

您可以使用chrome.history API扩展来读取历史记录,从那里我猜您可以获得上一页。

history extension source