When a deprecated URL is entered in Chrome it should be changed to an updated URL based on a regular expression or similar.
I would like to do the following
- Input a rule to the system that changes "olddomain.com" to "newdomain.com"
- Enter a URL like "olddomain.com/stuff"开发者_Python百科 in Chrome
- Chrome changes the url to "newdomain.com/stuff" and loads the page
- Repeat 2 and 3
But I don't know if it is even possible. Any hint on where to look?
The chosen solution were to develop a small extension for chrome, which inserts a button next to the address bar.
The primary code is given below.
manifest.json
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"background_page": "dostuff.html",
"browser_action": {
"default_icon": "icon.png"
},
"permissions": [
"tabs", "http://*.google.com/", "https://*.google.com/" ]
}
dostuff.html
<html>
<script>
var sciencedirectold = /sciencedirect.com\//;
var sciencedirectnew = "sciencedirect.com.proxy1-bib.sdu.dk:2048/";
function updateUrl(tab){
if(tab.url.match(sciencedirectold))
{
var newurl = tab.url.replace(sciencedirectold, sciencedirectnew);
chrome.tabs.update(tab.id, {url: newurl});
}
}
chrome.browserAction.onClicked.addListener(function(tab) {updateUrl(tab);});
</script>
</html>
Modified from the related answer
chrome.tabs.getSelected(null, function (tab){
var tabUrl = tab.url;
// First parameter is the regex, second one is the replacement.
tabUrl = tabUrl.replace(/([abcde])+/g, "$1");
chrome.tabs.update(tab.id, {url: tabUrl});
});
Note that I don't really know much about Chrome development, but I do know about JavaScript, so this would likely work.
You can use the chrome extension Edit Url by Regex.
It works by replacing the matching text with the new value.
Provide text to be matched in the Regex textbox and value to replace the matching text in the Value textbox. Then click the submit button. The tab url will now use the new url.
For your case:
Regex: olddomain.com
Value: newdomain.com
Screenshot for chrome extension popup where you need to provide input
精彩评论