From f3653f8bc78627853ec0c75c2a8c907c126b158a Mon Sep 17 00:00:00 2001 From: Jacob Strieb Date: Tue, 21 Jul 2020 16:57:11 -0400 Subject: [PATCH] Add absolute URL conversion to bookmarklet Also add explanatory comments --- .gitignore | 3 +++ bookmarklet.js | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 05070a1..322489d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ # Backed up copies of old files *.bak + +# Old HTML example files +*-old.html diff --git a/bookmarklet.js b/bookmarklet.js index b62a97c..08e8d22 100644 --- a/bookmarklet.js +++ b/bookmarklet.js @@ -1,4 +1,5 @@ -(() => { +javascript:(() => { + /* Generate the b64 API functions */ var b64 = (() => { function generateIndexDict(a) { let result = {}; @@ -47,7 +48,7 @@ } })(); - + /* Generate the page -> base64 API functions */ var api = { VERSION: "0.2.0", @@ -63,7 +64,16 @@ }, }; + /* Replace all relative URLs with absolute ones */ + Array.from(document.querySelectorAll("[src],[href]")).map(l => { + if ("src" in l) { + l.src = (new URL(l.src, document.baseURI)).href; + } else if ("href" in l) { + l.href = (new URL(l.href, document.baseURI)).href; + } + return l; + }); - javascript:window.open(api.getViewLink(document.documentElement.outerHTML), "_blank"); - + /* Redirect to the URL Page in a new tab */ + window.open(api.getViewLink(document.documentElement.outerHTML), "_blank"); })();