使用正则表达式或类似方式在Chrome中更改当前网址 [英] Change the current URL in Chrome using a regular expression or similar

查看:102
本文介绍了使用正则表达式或类似方式在Chrome中更改当前网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Chrome中输入弃用的网址时,应将其更改为基于正则表达式或类似网址更新的网址。



我想要执行以下操作


  1. 为系统输入一条将olddomain.com更改为newdomain.com的规则

  2. 在Chrome中输入一个类似olddomain.com/stuff的URL

  3. Chrome将URL更改为newdomain.com/stuff并加载页面

  4. 重复2和3

但我不知道这是否可能。任何暗示在哪里看?

解决方案

选择的解决方案是为Chrome开发一个小扩展,地址栏。



主要代码如下。

manifest.json

  {
name :我的第一个扩展,
版本:1.0,
description:我做的第一个扩展。,
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>


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?

解决方案

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>

这篇关于使用正则表达式或类似方式在Chrome中更改当前网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆