mrsamsa07 mrsamsa07 - 6 months ago 39
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: ""+searchstring})

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

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 "", run the search via context menu, and end up with a new tab on the following url:


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: ""+name+"&tld="+com})