I need some help with a quick bookmarklet

What it should do:
When pressed, the script checks to see if the url contains `“http://blocked.com-default.ws/”``

If so, it should find ‘url=’ from this address and take everything after it:
http://blocked.com-default.ws/?oI=10932090785&type=chromium-m&url=example.com

Then, it url decodes it(in this example it would be decoding ‘example.com’), like this: https://www.url-encode-decode.com/

Next, it Base64 encodes it, like this: https://www.base64encode.org/

Then, it adds that string to the end of this url and redirects you: http://openweb.gdn/index.php?q=

If not, it should take the whole url/address.

Then, it should Base64 encode it, like this: https://www.base64encode.org/

Then, it adds that string to the end of this url and redirects you: http://openweb.gdn/index.php?q=

Requirements:
Must function as a bookmarklet
Must work on Chrome

Hi @samgurdus, so what do you have so far and how can we help?

What I have tried
This script works decent for the ‘if so’, but doesn’t do the Base64 stuff, and is kind of hacky.

javascript:void function(){var o=window.location.toString();window.location=o.replace("blocked.com-default.ws/%3FoI=10932090785%26type=chromium-m%26url=","openweb.gdn/button/index.php%3Fq=")}();

Ah okay. Well you can get specific search values by creating an URLSearchParams object; this is certainly cleaner than using your own regular expression or something, and it works in chrome:

const params = new URLSearchParams(window.location.search)
// Get the url value, otherwise take the complete current location
const url = params.get('url') || window.location.href

You can then convert that url to base 64 with btoa() and do the redirect:

window.location.href = 'http://openweb.gdn/index.php?q=' + btoa(url)

Interesting. Where do I put the keyword I am searching for in the url?

That would be the .get()… e.g. with your example URL, you could get the following values:

const url = new URL('http://blocked.com-default.ws/?oI=10932090785&type=chromium-m&url=example.com')
const params = new URLSearchParams(url.search)

params.get('oI')   // 10932090785
params.get('type') // chromium-m
params.get('url')  // example.com

Ok, I am kind of confused. How would this work as a script? It sets the text after ‘url=’ as a variable and then does something?

You Base64-encode it and append it to the other URL as a search parameter, then redirect to that URL:

window.location.href = 'http://openweb.gdn/index.php?q=' + btoa(url)

Okay, I understand that part now. Could you explain what the top section does?

const params = new URLSearchParams(window.location.search)
// Get the url value, otherwise take the complete current location
const url = params.get('url') || window.location.href

Did you follow the link I gave you? It creates an URLSearchParams object from the query string part of the current location… then from that object it gets the url value, or if this value does not exist, it takes the full location instead.

Okay, I made a fiddle to lay this out. https://jsfiddle.net/samgurdus/koyb3eeb/ The part I don’t understand, is how it finds everything after url= EDIT: Also, where does it do the url decode?

This bit:

const url = params.get('url')

Sorry forgot about his… you can use decodeURIComponent() to do this. Together:

// Creates an URLSearchParams object from the query string part of the current location
const params = new URLSearchParams(window.location.search)
// Get the part after url=
const urlValue = params.get('url')
// If there is a match, decode it; otherwise just take the current location
const url = urlValue ? decodeURIComponent(urlValue) : window.location.href
// Redirect to openweb.gdn and append the Base64-encoded URL
window.location.href = 'http://openweb.gdn/index.php?q=' + btoa(url)

Thanks so much! It works great.

I want to modify this script a little, but am not sure the best way. I want to get the url param, and if there is one, add http in front of it, and if there isn’t then just take the whole address, before redirecting. I like the solution to just use the | | instead of a full ‘if else,’ so I was wondering how I could adapt it.

How could I do this?

What is the code that you currently have, that you want to modify?

I have this, which partially works. Only problem is that it makes everything http.

const params = new URLSearchParams(window.location.search)

const step1 = params.get('url') || window.location.href

var step2 = step1.replace('http://','');
var step3 = step2.replace('https://','');

url = "http://" + step3;

window.location.href = 'http://summitnet.pw/pages/frame?url=' + btoa(url)

What do you want it to do instead?

I want it to check if there is a url param in the window location, and if so, add http:// to the beginning and set it as a variable, address. If not, it should just take the whole location and set it as a variable, address.

if (hasURLParam(window.location)) {
  // add http to the beginning and set it as a variable address
} else {
  // set it as a variable address
}

In such cases, it’s better to move setting the variable address after the if statement.

if (hasURLParam(window.location)) {
  // add http to the beginning
}

// set it as a variable address

Can you please supply some examples of the expected behaviour?