|
| 1 | +const isNode = typeof window === 'undefined' |
| 2 | +const parse = isNode ? require('url').parse : browserParse |
| 3 | + |
| 4 | +const SCHEME_REGEX = /[a-z]+:\/\//i |
| 5 | +// 1 2 3 4 |
| 6 | +const VERSION_REGEX = /^(dat:\/\/)?([^/]+)(\+[^/]+)(.*)$/i |
| 7 | + |
| 8 | +function parseDatURL (str, parseQS) { |
| 9 | + // prepend the scheme if it's missing |
| 10 | + if (!SCHEME_REGEX.test(str)) { |
| 11 | + str = 'dat://' + str |
| 12 | + } |
| 13 | + |
| 14 | + var parsed, version = null, match = VERSION_REGEX.exec(str) |
| 15 | + if (match) { |
| 16 | + // run typical parse with version segment removed |
| 17 | + parsed = parse((match[1] || '') + (match[2] || '') + (match[4] || ''), parseQS) |
| 18 | + version = match[3].slice(1) |
| 19 | + } else { |
| 20 | + parsed = parse(str, parseQS) |
| 21 | + } |
| 22 | + if (isNode) parsed.href = str // overwrite href to include actual original |
| 23 | + parsed.version = version // add version segment |
| 24 | + console.log(parsed); |
| 25 | + return parsed |
| 26 | +} |
| 27 | + |
| 28 | +function browserParse (str) { |
| 29 | + return new URL(str) |
| 30 | +} |
| 31 | + |
| 32 | +var started = false; |
| 33 | + |
| 34 | +function startup() { |
| 35 | +if (started == false) { |
| 36 | +started = true; |
| 37 | + |
| 38 | +Array.from(document.querySelectorAll('[href^="dat://"]')).forEach(function(link){ |
| 39 | + console.log(parseDatURL(link.href)); |
| 40 | + //console.log(parseDatURL("dat://2714774d6c464dd12d5f8533e28ffafd79eec23ab20990b5ac14de940680a6fe/rotonde.js")); |
| 41 | + //link.href="about:blank"; |
| 42 | +}); |
| 43 | + |
| 44 | +Array.from(document.querySelectorAll('[src^="dat://"]')).forEach(function(link){ |
| 45 | + console.log(parseDatURL(link.src)); |
| 46 | + console.log("separator"); |
| 47 | + console.log(parseDatURL("dat://2714774d6c464dd12d5f8533e28ffafd79eec23ab20990b5ac14de940680a6fe+1/rotonde.js?lol#ten")); |
| 48 | + //link.src="about:blank"; |
| 49 | +}); |
| 50 | + |
| 51 | +}} |
| 52 | + |
| 53 | +window.setTimeout(function() { |
| 54 | +if (document.readyState === "complete") { |
| 55 | +startup(); |
| 56 | +} else { |
| 57 | +document.addEventListener("DOMContentLoaded", startup, false); |
| 58 | +document.addEventListener("load", startup, false); |
| 59 | +window.addEventListener("load", startup, false); |
| 60 | +} |
| 61 | + |
| 62 | +}, 0); |
0 commit comments