7

I have this so far:

chrome.tabs.getSelected(null, function(tab) 
{
    var title = tab.title;
    var btn = '<a href="' + tab.url + '" onclick="save(\'' + title + '\');"> ' + title + '</a>';
        
    if(tab.url.match('/http:\/\/www.example.com\/version.php/i')) 
    {
        document.getElementById('link').innerHTML = '<p>' + btn + '</p>';
    }
});

Basically it should match the domain within this:

http://www.example.com/version.php?*

Anything that matches that even when it includes something like version.php?ver=1, etc

When I used the code above of mine, it doesn't display anything, but when I remove the if statement, it's fine but it shows on other pages which it shouldn't only on the matched URL.

EDIT:

if(tab.url.match(/http:\/\/www.example.com\/version.php/i)) 
{
    document.getElementById('link').innerHTML = '<p>' + btn + '</p>';
}

Doesn't even work somehow...

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
BonjourHolaOla
  • 135
  • 3
  • 4
  • 8

4 Answers4

6

Try this:

if(tab.url.match(/http\:\/\/www\.example\.com\/version\.php/i)) 
{
    //...
}
Aryan Beezadhur
  • 3,651
  • 3
  • 16
  • 39
limc
  • 38,266
  • 19
  • 97
  • 142
1

Remove the quotes. Opera DargonFly gives me:

>>> 'http://www.example.com/version.php'.match(/^http:\/\/www\.example.com\/version\.php/i) 
[object Array]
Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Artefacto
  • 93,596
  • 16
  • 191
  • 218
0

You are creating the RegExp object, but you're not matching it against anything. See here for how to use it (by the way, the syntax is also wrong, it's either /expression/modifiers or RegExp(expression, modifiers)).

Artefacto
  • 93,596
  • 16
  • 191
  • 218
0

match() should take a RegExp object as argument, not a string. You could probably just remove the single quotes '' from around the expression to make it work. For future reference, you should also escape the periods . in the expression (as it is they will match any single character), and insert a ^ in the beginning to only allow this match in the beginning of the URL.

Arkku
  • 39,548
  • 10
  • 58
  • 80