When I put most of the code into a function, I want to know when it’s working as expected, and what needs to be fixed.
function getHref(search, href) {
const params = new URLSearchParams(search)
const step1 = params.get('url') || href
var step2 = step1.replace('http://','');
var step3 = step2.replace('https://','');
url = "http://" + step3;
return url;
}
var url = getHref(window.location.search, window.location.href);
window.location.href = 'http://summitnet.pw/pages/frame?url=' + btoa(url);
We can now simplify things down to what occurs based on just the search and href parameters.
The following test shows what happens when things are working right:
describe("getHref tests", function () {
it("gives the http search value", function () {
var url = "http://www.google.com";
var search = "url=" + url;
var href = "http://example.com";
expect(getHref(search, href)).toBe(url);
});
});
And when there’s no protocol on there, one gets added:
it("adds http:// when protocol is missing", function () {
var url = "www.google.com";
var search = "url=" + url;
var href = "http://example.com";
expect(getHref(search, href)).toBe("http://" + url);
});
And when there’s no url, it gives the href instead.
it("gives the href when search is empty", function () {
var search = "";
var href = "http://example.com";
expect(getHref(search, href)).toBe(href);
});
Those all work as expected. The situation that you want to be dealt with though is when https is in the protocol. You want it to remain as https, which is shown by this test code:
it("gives the https search value", function () {
var url = "https://www.google.com";
var search = "url=" + url;
var href = "http://example.com";
expect(getHref(search, href)).toBe(url);
});
I am trying to use this as a bookmarklet, so I will be setting the result as a param in the url, before redirecting. In this case, I won’t need to make it a function, so how could I just set it as a param?
I am trying to use this as a bookmarklet, so I will be setting the result as a param in the url, before redirecting. In this case, I won’t need to make it a function, so how could I just set it as a param?