mrsamsa07 mrsamsa07 - 3 months ago 7
Javascript Question

Chrome extension: splitting selection into two search strings

Creating a context menu chrome extension, to search highlighted text through various systems.

Currently, searches are run by taking highlighted text and adding it to the end of a search URL. For example:

{
var searchstring = info.selectionText;
chrome.tabs.create({url: "http://www.intodns.com/"+searchstring})
}


The search URL for eNom domain management splits the search term into two parts, SLD and TLD, as follows:

http://www.enom.com/domains/control-panel/default.aspx?sld=DOMAIN&tld=COM

Trying to figure out a way to split the selection text into two parts, sld and tld, then plug each part into the corresponding spot in the search URL.

End result would allow a user to highlight "stackoverflow.com", run the search via context menu, and end up with a new tab on the following url:

http://www.enom.com/domains/control-panel/default.aspx?sld=stackoverflow&tld=com

Answer

I've tried to make it more robust; it shouldn't include "www." when it's at the beginning, but if there's a "www." later on for some reason it'll allow it.

var searchstring = info.selectionText;

var parts = searchstring.match(/^(?:\w+:\/\/\/?)?(?:www\.)?([^\.]+)\.(.+)/i);

var name = parts[1], com = parts[2];

chrome.tabs.create({url: "http://www.enom.com/domains/control-panel/default.aspx?sld="+name+"&tld="+com})
Comments