Skip to content

Instantly share code, notes, and snippets.

@igrep
Last active September 9, 2024 08:49
Show Gist options
  • Save igrep/d3476e235f5078ee69d19e7af5d47114 to your computer and use it in GitHub Desktop.
Save igrep/d3476e235f5078ee69d19e7af5d47114 to your computer and use it in GitHub Desktop.
javascript:(()=>{"use strict";let leftAvailable=255;function fnv1a(t){let l=2166136261;for(let e=0;e<Math.ceil(t.length/2);++e){var n=t.charCodeAt(2*e),d=t.charCodeAt(2*e+1),d=isNaN(d)?0:d;l=(l^(n^d<<16))*16777619}return l}leftAvailable-=".html".length;const re=/[\uD83C-\uDFFF\u2600-\u26FF\\\/:*?"<>|]+/g,titleFsSafe=document.title.replace(re,"").trim(),{href,hostname,pathname,search,hash}=location,urlFsSafe=(""+hostname+pathname+search+hash).replace(re,"_"),fnvHash=fnv1a(""+document.title+href).toString(32);leftAvailable-=fnvHash.length;let safeTitle=titleFsSafe,safeUrl=urlFsSafe;const titleUtf8=(new TextEncoder).encode(titleFsSafe.normalize("NFD")),sumLength=titleUtf8.length+urlFsSafe.length;if(leftAvailable<sumLength){const i=Math.ceil(titleUtf8.length/sumLength*leftAvailable);safeTitle=(new TextDecoder).decode(titleUtf8.slice(0,i)),safeUrl=urlFsSafe.slice(0,leftAvailable-i-1)}document.head.dataset.title4pdforiginaltitle&&document.head.dataset.title4pdforiginalurl===href||(document.head.dataset.title4pdforiginaltitle=document.title,document.title=""+safeUrl+safeTitle+fnvHash,document.head.dataset.title4pdforiginalurl=href);const originalTitle=document.head.dataset.title4pdforiginaltitle,dlElement=document.createElement("dl"),dtUrlElement=document.createElement("dt"),ddUrlElement=(dtUrlElement.textContent="URL",dlElement.appendChild(dtUrlElement),document.createElement("dd")),dtTitleElement=(ddUrlElement.textContent=href,dlElement.appendChild(ddUrlElement),document.createElement("dt")),ddTitleElement=(dtTitleElement.textContent="Title",dlElement.appendChild(dtTitleElement),document.createElement("dd")),dtPdfNameElement=(ddTitleElement.textContent=originalTitle,dlElement.appendChild(ddTitleElement),document.createElement("dt")),ddPdfNameElement=(dtPdfNameElement.textContent="PDF Name",dlElement.appendChild(dtPdfNameElement),document.createElement("dd")),a=(ddPdfNameElement.textContent=document.title,dlElement.appendChild(ddPdfNameElement),document.createElement("a"));a.href=URL.createObjectURL(new Blob([dlElement.outerHTML],{type:"text/html;charset=UTF-8"})),a.download=document.title+".html",a.click()})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment