问题
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