Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/216643-replace-url-domain/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125598
Share on other sites

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"?

Link to comment
https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125601
Share on other sites

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125605
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125612
Share on other sites

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! :)

Link to comment
https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125840
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1125895
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/216643-replace-url-domain/#findComment-1126634
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.