views:

300

answers:

2

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" 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?

A: 

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.

MiffTheFox
A: 

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>
midtiby

related questions