AlphaWolf Posted October 23, 2010 Share Posted October 23, 2010 Hi, I've got the following code: if (myVolume == '.co.uk') { event.target.browserWindow.activeTab.url = currentURL.replace(/\.com/ig, ".co.uk"); } I would like to alter it so it changes any URL Domain, and not only .com. I've looked around, but can't find a solution. Thanks, Joseph Duffy Quote Link to comment https://forums.phpfreaks.com/topic/216643-replace-url-domain/ Share on other sites More sharing options...
.josh Posted October 23, 2010 Share Posted October 23, 2010 first off, that regex is flawed. It will match and replace all ".com" instances in a URL. So for instance if you have something like www.comedy.com you will end up with www.co.uk.edy.co.uk 2nd, there is no easy way to do what you're asking because a domain can have more than one suffix. Could be a single extension like .com or double like .co.uk and there are a ton of them out there and the hostname itself (minus extensions) are completely arbitrary. In short, there is no reliable way to replace all domain suffixes with .co.uk. The "easiest" thing to do would be to create a whitelist of all the domain suffixes you expect to show up and match against that...like if you have a handful of known domains you are wanting to redirect or whatever. But if you are looking to do "any" or "all" and it can be any or all domain suffixes then...I'm afraid you're out of luck. Quote Link to comment https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125598 Share on other sites More sharing options...
AlphaWolf Posted October 23, 2010 Author Share Posted October 23, 2010 first off, that regex is flawed. It will match and replace all ".com" instances in a URL. So for instance if you have something like www.comedy.com you will end up with www.co.uk.edy.co.uk 2nd, there is no easy way to do what you're asking because a domain can have more than one suffix. Could be a single extension like .com or double like .co.uk and there are a ton of them out there and the hostname itself (minus extensions) are completely arbitrary. In short, there is no reliable way to replace all domain suffixes with .co.uk. The "easiest" thing to do would be to create a whitelist of all the domain suffixes you expect to show up and match against that...like if you have a handful of known domains you are wanting to redirect or whatever. But if you are looking to do "any" or "all" and it can be any or all domain suffixes then...I'm afraid you're out of luck. I've just tried my current code on "www.companies.com". It changes it "www.co.ukpanies.com". Interesting that it doesn't make it "www.co.ukpanies.co.uk" :S I'm thinking to ask it to change the last one (Although it could have "www.domain.com/something/.com"?), or the second one, so it will only ever change the correct one? I don't think you could have "subdomain.com.domain.com"? Quote Link to comment https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125601 Share on other sites More sharing options...
.josh Posted October 23, 2010 Share Posted October 23, 2010 you can have as many subdomain levels as you want..... a.b.c.d.e.domain.com but yeah...I find it interesting that your .replace has the global modifier and yet only replaces the first instance...but you can for instance have urls like www.somesite.com/page.html?ref=http://www.someothersite.com and then what? Quote Link to comment https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125605 Share on other sites More sharing options...
.josh Posted October 23, 2010 Share Posted October 23, 2010 argh too long to edit, new post: anyways, as far as that replace... for instance: <script language="Javascript"> var x = "www.company.com"; alert(x); alert(x.replace(/\.com/ig, ".co.uk")); </script> That replaces both instances just fine. There's got to be more to your code than you've posted or maybe you didn't post the right code. Quote Link to comment https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125612 Share on other sites More sharing options...
AlphaWolf Posted October 24, 2010 Author Share Posted October 24, 2010 argh too long to edit, new post: anyways, as far as that replace... for instance: <script language="Javascript"> var x = "www.company.com"; alert(x); alert(x.replace(/\.com/ig, ".co.uk")); </script> That replaces both instances just fine. There's got to be more to your code than you've posted or maybe you didn't post the right code. Current Code: // Get the current tab's URL. This URL can be undefined if the // page is blank or the user is at a page like Top Sites. To // get the URL, the extension must have permissions to access // the page at that URL. The Web Site Access Level setting in // the Extension Builder controls this. This particular extension // wants an access level of "All" so that it can change all URLs. var currentURL = event.target.browserWindow.activeTab.url; // Sets convert1 to the users prefernces, alowing changes of .co.uk, .fr etc. var convert1 = safari.extension.settings.URLSettings1; var convert2 = safari.extension.settings.URLSettings2; if (currentURL) //Converts URL 1 (Default .com) to URL 2 (Default .co.uk) if (currentURL.indexOf(convert1) !=-1) { event.target.browserWindow.activeTab.url = currentURL.replace(convert1, convert2); } if (currentURL.indexOf(convert2) !=-1) { event.target.browserWindow.activeTab.url = currentURL.replace(convert2, convert1); } Thanks for the swift replies and help! Quote Link to comment https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125840 Share on other sites More sharing options...
.josh Posted October 24, 2010 Share Posted October 24, 2010 okay yah...so the actual replace you are using isn't an actual pattern and doesn't have that global modifier you previously showed, so yeah, it's just a straight string replace, that's why it's only changing the first instance of .com to .co.uk. the better thing for your code to be doing is a replace with an end of string anchor on the hostname instead of full url, and then rebuilding the full url with protocol, hostname, pathname and query string (other js environment variables). This will make it only replace the last instance of the hostname only....however, this doesn't address your issue of wanting to replace any domain suffix with .co.uk which as I mentioned, is an impossible task, because of the sheer size of available suffixes.....unless you only expect a handful of domain suffixes to come up (which I would not bother trying to do unless that list is like 20-50 tops) to match against. Quote Link to comment https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125895 Share on other sites More sharing options...
AlphaWolf Posted October 26, 2010 Author Share Posted October 26, 2010 okay yah...so the actual replace you are using isn't an actual pattern and doesn't have that global modifier you previously showed, so yeah, it's just a straight string replace, that's why it's only changing the first instance of .com to .co.uk. the better thing for your code to be doing is a replace with an end of string anchor on the hostname instead of full url, and then rebuilding the full url with protocol, hostname, pathname and query string (other js environment variables). This will make it only replace the last instance of the hostname only....however, this doesn't address your issue of wanting to replace any domain suffix with .co.uk which as I mentioned, is an impossible task, because of the sheer size of available suffixes.....unless you only expect a handful of domain suffixes to come up (which I would not bother trying to do unless that list is like 20-50 tops) to match against. I'm not longer that bothered about replacing any domain suffix (Is it domain suffix or URL domain?), I'm happy with the way it is. Can you give me any pointers on how to "replace with an end of string anchor on the hostname"? I'm assuming this would change "www.commedy.com/funny/.com" to "www.commedy.co.uk/funny/.com"? Thanks for all the help, much appreciated! Joseph Duffy Quote Link to comment https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1126634 Share on other sites More sharing options...
AlphaWolf Posted October 30, 2010 Author Share Posted October 30, 2010 Bump? Quote Link to comment https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1128478 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.