Last active
March 18, 2024 16:23
-
-
Save mutoo/0acc57192ff4de710f55043581da99b1 to your computer and use it in GitHub Desktop.
Get all source map links on page. Usage: create a snippet on `chrome devtools > sources > snippets` and run it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const scriptTags = Array.from(document.querySelectorAll('script')); | |
const scriptSrcs = scriptTags.map(tag=>tag.src).filter(i=>i); | |
const SOURCE_MAPPING_URL = "//# sourceMappingURL="; | |
const maybeSourceMappingURLs = await Promise.allSettled(scriptSrcs.map(async(src)=>{ | |
const code = await fetch(src).then(resp=>resp.text()); | |
const lastLine = code.split("\n").pop(); | |
if (!lastLine.startsWith(SOURCE_MAPPING_URL)) { | |
throw new Error('This scripts don\'t have sourcemap'); | |
} | |
const srcUrl = new URL(window.location.href); | |
let sourceMappingURL = lastLine.slice(SOURCE_MAPPING_URL.length); | |
if (sourceMappingURL.startsWith("http")) { | |
return sourceMappingURL; | |
} | |
if (src.startsWith("http")) { | |
return new URL(sourceMappingURL,src).href; | |
} | |
if (sourceMappingURL.startsWith("/")) { | |
return new URL(sourceMappingURL,window.location.href).href; | |
} | |
const basePath = new URL(src,window.location.href).href; | |
return new URL(sourceMappingURL,basePath).href; | |
} | |
)); | |
const sourceMappingURLs = maybeSourceMappingURLs.filter(entry=>entry.status === 'fulfilled').map(entry=>entry.value); | |
console.log(sourceMappingURLs.join("\n")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment