-1

I want to know how I can create a script that replaces "https://www." in the Reddit URL, with "ps." when I click a bookmark. Does anyone know how I can do this? My programming knowledge is pretty limited.

  • So you have 3 separate issues.... how to get the url into a variable, how to replace part of a string and how to change the url to the modified string. Take each task and research them separately then start combining them. Come back when you actually have some real code and it isn't working as expected – charlietfl Dec 16 '17 at 00:48
  • Alright will do. Thanks for breaking it down for me. – Michael Rahmani Dec 16 '17 at 00:53
  • The hardest part about this problem is determining how *you* are using those terms (often misused). Add some screenshots and/or sketches showing what you would do manually. – Brock Adams Dec 16 '17 at 03:43
  • Or, you guys could just tell him to google "bookmarklet" then give him a short script that does exactly what he wants. But you know, NOT giving him an answer is also fun I guess. – Simon Hyll Dec 16 '17 at 04:22

1 Answers1

1

What you're talking about it a "bookmarklet".

In Chrome, open up the bookmark bar. Right click it, press "Add page", give it a name, then paste in as a value (instead of a URL) a javascript function, e.g. like this (taken from crossbrowsertesting.com):

javascript:(function(){if(typeof cbt_script=='undefined'){cbt_script=document.createElement('SCRIPT');cbt_script.type='text/javascript';cbt_script.src='https://crossbrowsertesting.com/cbt_bookmarklet.js.php?random='+(new Date()).getTime();document.getElementsByTagName('head')[0].appendChild(cbt_script);}else{showCBTbookmarklet();}})();

So now the question is only "how do i make javascript edit the current adress?" Well that's easy, just use window.location.href = '';

so for example:

javascript:(function(){window.location.href='https://google.com'})();

Will take you to https://google.com.

So now we make the javascript take the current page and transform it somewhat:

// The weird structure of the function is because it's a "self running"
// function, they look like this (function(){/*code*/})();
(function () {
var currentUrl = window.location.href;
var newUrl = currentUrl.replace("https://", "https://ps.");
window.location.href = newUrl;
}();

Or in bookmarklet form and shortened down:

javascript:(function(){location.replace(window.location.href.replace("https://","https://ps."))})();

This would turn e.g. https://google.com into https://ps.google.com when you press the bookmark.

Note that you need http or https at the start, otherwise the location.replace function won't open it the way you want it to.

Simon Hyll
  • 3,265
  • 3
  • 24
  • 44
  • Hi, firstly, I just want to say thanks for taking the time for explaining this. I tried executing the code for the bookmarklet and it didn't seem to be doing anything. So I replaced the "ps://" with "ps." and it seemed to be working a little bit better but not working correctly. It was instead adding ps.google.com to end of the already existing URL, instead of replacing the old url with the new one. So clicking the bookmarklet would convert https://google.com/ to https://www.google.com/ps.www.google.com/ which is not correct. I think there is something wrong with the replace function. – Michael Rahmani Dec 16 '17 at 18:05
  • I updated the script, I thought you might've wanted ps://, but if you want ps. to be prefixed the replace function would be replace('https://','https://ps.'), which would result in e.g. https://google.com being turned into https://ps.google.com. – Simon Hyll Dec 16 '17 at 19:59
  • I dunno dude. It still doesn't seem to be working. It's just adding ps.google.com onto the already exist url. Ok so this page on Reddit https://www.reddit.com/r/gifs/comments/7k7mbb/amazing_save/ I click the bookmarklet and it converts to https://www.reddit.com/r/gifs/comments/7k7mbb/amazing_save/ps.reddit.com/r/gifs/comments/7k7mbb/amazing_save/ which is not the right thing. It's supposed to just be replacing https:// in the beginning of the url with ps. Thanks for the help though. Also I upvoted you but it didn't get recorded because I don't have 15 reputation. – Michael Rahmani Dec 16 '17 at 20:32
  • Oh, It works now! Thank you so much dude! Your latest edit solved the issue. Thanks! Marked your answer as solved. – Michael Rahmani Dec 16 '17 at 20:35
  • No problem, 'tis the season of giving after all :) – Simon Hyll Dec 16 '17 at 20:39
  • Yep exactly dude. And the way you broke it down and explained it helped me learn how it works also – Michael Rahmani Dec 16 '17 at 20:41