|
| 1 | +'use strict' |
| 2 | +/* eslint-env browser, webextensions */ |
| 3 | + |
| 4 | +/* |
| 5 | + * This content script detects IPFS-related protocols in `href` and `src` |
| 6 | + * attributes and replaces them with URL at the user-specified public HTTP gateway. |
| 7 | + * Note that if IPFS API is online, HTTP request will be redirected to custom gateway. |
| 8 | + * |
| 9 | + * For more background see: https://github.com/ipfs/ipfs-companion/issues/286 |
| 10 | + * |
| 11 | + * Test page: http://bit.ly/2hXiuUz |
| 12 | + */ |
| 13 | + |
| 14 | +;(function (alreadyLoaded) { |
| 15 | + if (alreadyLoaded) { |
| 16 | + return |
| 17 | + } |
| 18 | + |
| 19 | + // Limit contentType to "text/plain" or "text/html" |
| 20 | + if (document.contentType !== undefined && document.contentType !== 'text/plain' && document.contentType !== 'text/html') { |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + // prevent double init |
| 25 | + window.ipfsCompanionNormalizedUnhandledProtocols = true |
| 26 | + |
| 27 | + // XPath selects all elements that have `href` of `src` attribute starting with one of IPFS-related protocols |
| 28 | + const xpath = ".//*[starts-with(@href, 'ipfs://') or starts-with(@href, 'ipns://') or starts-with(@href, 'dweb:') " + |
| 29 | + " or starts-with(@src, 'ipfs://') or starts-with(@src, 'ipns://') or starts-with(@src, 'dweb:')]" |
| 30 | + |
| 31 | + const pubGwURL = window.ipfsCompanionPubGwURL |
| 32 | + |
| 33 | + function init () { |
| 34 | + // initial run |
| 35 | + normalizeTree(document.body) |
| 36 | + |
| 37 | + // listen for future DOM changes |
| 38 | + new MutationObserver(function (mutations) { |
| 39 | + mutations.forEach(function (mutation) { |
| 40 | + if (mutation.type === 'childList') { |
| 41 | + for (let addedNode of mutation.addedNodes) { |
| 42 | + if (addedNode.nodeType === Node.ELEMENT_NODE) { |
| 43 | + setTimeout(() => normalizeTree(addedNode), 0) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + }) |
| 48 | + }).observe(document.body, { |
| 49 | + characterData: false, |
| 50 | + childList: true, |
| 51 | + subtree: true |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + function normalizeElement (element) { |
| 56 | + if (element.href) { |
| 57 | + // console.log('normalizeElement.href: ' + element.href) |
| 58 | + element.href = normalizeAddress(element.href) |
| 59 | + } else if (element.src) { |
| 60 | + // console.log('normalizeElement.src: ' + element.src) |
| 61 | + element.src = normalizeAddress(element.src) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // replaces unhandled protocol with a regular URL at a public gateway |
| 66 | + function normalizeAddress (addr) { |
| 67 | + return addr |
| 68 | + .replace(/^dweb:\//i, pubGwURL) // dweb:/ipfs/Qm → /ipfs/Qm |
| 69 | + .replace(/^ipfs:\/\//i, `${pubGwURL}ipfs/`) // ipfs://Qm → /ipfs/Qm |
| 70 | + .replace(/^ipns:\/\//i, `${pubGwURL}ipns/`) // ipns://Qm → /ipns/Qm |
| 71 | + } |
| 72 | + |
| 73 | + function normalizeTree (root) { |
| 74 | + const xpathResult = document.evaluate(xpath, root, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) |
| 75 | + let i = 0 |
| 76 | + function continuation () { |
| 77 | + let node = null |
| 78 | + let counter = 0 |
| 79 | + while ((node = xpathResult.snapshotItem(i++))) { |
| 80 | + const parent = node.parentNode |
| 81 | + // Skip if no longer in visible DOM |
| 82 | + if (!parent || !document.body.contains(node)) continue |
| 83 | + normalizeElement(node) |
| 84 | + if (++counter > 10) { |
| 85 | + return setTimeout(continuation, 0) |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + window.requestAnimationFrame(continuation) |
| 90 | + } |
| 91 | + |
| 92 | + init() |
| 93 | +}(window.ipfsCompanionNormalizedUnhandledProtocols)) |
0 commit comments