开发者

Change the current URL in Chrome using a regular expression or similar

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

  1. Input a rule to the system that changes "olddomain.com" to "newdomain.com"
  2. Enter a URL like "olddomain.com/stuff"开发者_Python百科 in Chrome
  3. Chrome changes the url to "newdomain.com/stuff" and loads the page
  4. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜