在浏览器操作中修改当前选项卡的URL的TLD [英] Modify TLD of URL of current tab in a browser action

查看:130
本文介绍了在浏览器操作中修改当前选项卡的URL的TLD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使浏览器操作在.com(或任何TLD在那里)和URL的/foo.bar部分之间插入一些文本。



例如,如果我在stackoverflow.com/questions上,我希望能够在浏览器中按下按钮并自动重定向到stackoverflow.com.my.text / questions



按钮显示在正确的位置(并且看起来很漂亮),但什么都不做。



我模拟了我这个例子。这是我现在有:

manifest.json

  { 
manifest_version:2,

name:My Extension,
version:1.0,

background :{scripts:[insert.js]},
browser_action:{default_icon:my_icon.png},
permissions:[tabs,http :// * / *,https:// * / *]

}

和insert.js

  chrome.browserAction.onClicked.addListener(function(tab){

var url = tab.url;
var tld = /\.\w+\//;
var url2 = tld.exec(url);

var break = url.indexOf(url2);
var break1 = break + url2 [0] .length - 1;

var url1 = url.substring(0,break1) ;
var url3 = url.substring(break1,url.length);

var mytext =.my.text;
var urlNew = url1 + mytext + url3;

chrome.tabs.update(tab.id,{url:urlNew});
});

我可能错过了一些非常明显的东西,但我对JavaScript基本一无所知。



我通过修改 http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_regexp_test2 ,所以我知道它匹配正确的正则表达式并且吐出正确的字符串。



我搜索了一会儿,找不到任何东西来指导我。为什么我发布工作的例子,但我的代码不?

解决方案

如果你想学习任何东西,忘记w3schools.com 并使用信誉良好的来源,如MDN。



那么,在看了你的代码之后,我已经看到了一个明显的错误:

  var url = tab.url; 
var tld = /\.\w+\//;
var url2 = tld.exec(url);

var break = url.indexOf(url2);

正则表达式的exec方法返回一个数组(或 null )。
字符串indexOf方法需要一个字符串。这些参数不匹配,所以得到的结果可能与您的期望不符。


我正在尝试制作浏览器将在.com(或其他任何TLD)和URL的/foo.bar部分之间插入一些文本。


然后使用字符串的替换方法

  chrome.browserAction.onClicked.addListener(function(tab){
var url = tab。 url;

var urlNew = url.replace(/(\.\w +)(\ /)/,'$ 1.my.text $ 2');

chrome.tabs.update(tab.id,{url:urlNew});
});

$ 1与捕获的组 \.\\w +

$ 2匹配捕获的组 \ /



<注意: \ w + 匹配 [a-zA-Z0-9 _] + ,而a TLD也可以包含连字符( - )(请参阅 https:// data.iana.org/TLD/tlds-alpha-by-domain.txt )。如果您想要匹配这些网址,您可能还需要调整常规操作。


I'm trying to make a browser action that will insert some text between ".com" (or whatever TLD is there) and the "/foo.bar" parts of a URL.

For example, if I'm on "stackoverflow.com/questions" I want to be able to press a button in my browser and automatically be redirected to "stackoverflow.com.my.text/questions"

The button shows up in the right place (and looks pretty) but does nothing.

I modeled my code after this example. This is what I currently have:

manifest.json

    {
  "manifest_version": 2,

   "name": "My Extension",
   "version": "1.0",

   "background": { "scripts": [ "insert.js" ] },
   "browser_action": { "default_icon": "my_icon.png" },
   "permissions": [ "tabs", "http://*/*", "https://*/*" ]

}

and insert.js

chrome.browserAction.onClicked.addListener(function(tab) {

    var url = tab.url; 
    var tld = /\.\w+\//;
    var url2 = tld.exec(url);

    var break = url.indexOf(url2);
    var break1 = break + url2[0].length - 1;

    var url1 = url.substring(0, break1);
    var url3 = url.substring(break1, url.length);

    var mytext = ".my.text";
    var urlNew = url1 + mytext + url3;

    chrome.tabs.update(tab.id, {url: urlNew});
});

I'm probably missing something really obvious, but I know basically nothing about JavaScript.

I did test the script itself by modifying the code at http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_regexp_test2 so I know it matchs the right regular expression and spits out the right string.

I searched around for a while and couldn't find anything to guide me. Why does the example I posted work, but my code does not?

解决方案

If you want to learn anything, forget about w3schools.com and use reputable sources such as MDN.

That having said, after glancing at your code, I already see one blatant mistake:

var url = tab.url; 
var tld = /\.\w+\//;
var url2 = tld.exec(url);

var break = url.indexOf(url2);

The regex's exec method returns an array (or null if no match is found).
The string's indexOf method takes a string. These arguments do not match, so the result you're getting will probably not match your expectations.

I'm trying to make a browser action that will insert some text between ".com" (or whatever TLD is there) and the "/foo.bar" parts of a URL.

Then use the string's replace method:

chrome.browserAction.onClicked.addListener(function(tab) {
    var url = tab.url; 

    var urlNew = url.replace(/(\.\w+)(\/)/, '$1.my.text$2');

    chrome.tabs.update(tab.id, {url: urlNew});
});

"$1" matches the captured group \.\w+,
"$2" matches the captured group \/.

Note: \w+ matches [a-zA-Z0-9_]+, while a TLD can also contain a hyphen ("-") (see https://data.iana.org/TLD/tlds-alpha-by-domain.txt). You might want to adjust your regular experssion if you want to match these URLs as well.

这篇关于在浏览器操作中修改当前选项卡的URL的TLD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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