Chrome extension that captures and redirects anything typed in the URL/search bar

拈花ヽ惹草 提交于 2019-12-25 06:04:35

问题


I would like to create an extension that will manipulate anything (mathching a pattern) typed in the URL bar and make an apriopriate redirect.

Being more specific: I would like to type an URL of such format: _http://<something>. I would like chrome to redirect this http://<something>.

Here is what I came up with (basing on this answer):

manifest.json

{
  "name": "_Replace",
  "description": "Replace '_http' with 'http'",
  "version": "1.0",
  "manifest_version": 2,
  "background": {"scripts":["background.js"]},
  "permissions": [
    "webRequest",
    "<all_urls>",
    "webRequestBlocking"
  ]
}

background.js

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {

        var url = details.url;
        console.log(url);
        if(url[0] == "_")
         return {redirectUrl: url.substr(1)};
    },
    {
        urls: [
            "<all_urls>"
        ],
        types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
    },
    ["blocking"]
);

Unfortunatelly since "_http://xxx" in a valid URL, google would automatically treat this as a search phrase and redirect me to google search. How could I prevent searching and make a redirect I want?

来源:https://stackoverflow.com/questions/33417206/chrome-extension-that-captures-and-redirects-anything-typed-in-the-url-search-ba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!