简体   繁体   English

我可以用javascript按下Chrome中的“后退”按钮吗?

[英]Can i push the “back” button in chrome with javascript

I would like help to understand how I can go to the previous page in chrome if i try to join m.facebook.com 如果我尝试加入m.facebook.com,我想帮助了解如何在chrome中转到上一页

Let me show you my code until now: 我现在告诉你我的代码:

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"]
);

manifest 表现

{
    "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"
    ]
}

With this code I can "block" m.facebook.com and redirect to "www.google.com" from my Chrome browser when i have installed the extension. 使用此代码,我可以“阻止”m.facebook.com并在安装扩展程序时从我的Chrome浏览器重定向到“www.google.com”。 But i want just to go to the previous page. 但我只想转到上一页。

UPDATE1: UPDATE1:

Like christopher suggested i tried these codes 像克里斯托弗建议我尝试这些代码

added to the manifest permissions "history" 添加到清单权限“历史记录”

and i made the background.js like this 我做了这样的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"]
);

But i cant figure out my mistake. 但我无法弄清楚我的错误。

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

Is this what you mean? 你是这个意思吗?

You can use the chrome.history API extension to read the history, from there I guess you can get the previous page. 你可以使用chrome.history API扩展来读取历史记录,从那里我猜你可以得到上一页。

history extension source 历史扩展源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM