From 4000aba15ee24940c8330f5de771e6bf50f42148 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 12 Dec 2019 17:05:36 +0100 Subject: [PATCH] fix: remove normalizeLinksContentScript The script was responsible for overriding all src and href in DOM of every page to ensure content addressed with ipfs:// URL loads. Looking back, it does not align well with our upgrade path, and the result across browser vendors was buggy. While it allowed loading simple images, it did not help with broken XHR and iframe quirks and introduced unnecessary battery drain. Due to this, it is better to remove it and advise users to use content-addressing at a public gateway. This way everything works for people without IPFS client, and browsers with IPFS support can easily detect it and upgrade the call. --- add-on/_locales/en/messages.json | 2 +- .../normalizeLinksWithUnhandledProtocols.js | 93 ------------------- add-on/src/lib/ipfs-companion.js | 26 +----- webpack.config.js | 3 +- 4 files changed, 4 insertions(+), 120 deletions(-) delete mode 100644 add-on/src/contentScripts/normalizeLinksWithUnhandledProtocols.js diff --git a/add-on/_locales/en/messages.json b/add-on/_locales/en/messages.json index a923b424a..af7c9cded 100644 --- a/add-on/_locales/en/messages.json +++ b/add-on/_locales/en/messages.json @@ -380,7 +380,7 @@ "description": "An option title on the Preferences screen (option_catchUnhandledProtocols_title)" }, "option_catchUnhandledProtocols_description": { - "message": "Enables support for ipfs://, ipns:// and dweb: by normalizing links and requests done with unhandled protocols", + "message": "Enables provisional support for ipfs://, ipns:// and dweb: by redirecting unhandled address bar requests to an HTTP gateway", "description": "An option description on the Preferences screen (option_catchUnhandledProtocols_description)" }, "option_linkify_title": { diff --git a/add-on/src/contentScripts/normalizeLinksWithUnhandledProtocols.js b/add-on/src/contentScripts/normalizeLinksWithUnhandledProtocols.js deleted file mode 100644 index 3fe9a206a..000000000 --- a/add-on/src/contentScripts/normalizeLinksWithUnhandledProtocols.js +++ /dev/null @@ -1,93 +0,0 @@ -'use strict' -/* eslint-env browser, webextensions */ - -/* - * This content script detects IPFS-related protocols in `href` and `src` - * attributes and replaces them with URL at the user-specified public HTTP gateway. - * Note that if IPFS API is online, HTTP request will be redirected to custom gateway. - * - * For more background see: https://github.com/ipfs/ipfs-companion/issues/286 - * - * Test page: http://bit.ly/2hXiuUz - */ - -;(function (alreadyLoaded) { - if (alreadyLoaded) { - return - } - - // Limit contentType to "text/plain" or "text/html" - if (document.contentType !== undefined && document.contentType !== 'text/plain' && document.contentType !== 'text/html') { - return - } - - // prevent double init - window.ipfsCompanionNormalizedUnhandledProtocols = true - - // XPath selects all elements that have `href` of `src` attribute starting with one of IPFS-related protocols - const xpath = ".//*[starts-with(@href, 'ipfs://') or starts-with(@href, 'ipns://') or starts-with(@href, 'dweb:') " + - " or starts-with(@src, 'ipfs://') or starts-with(@src, 'ipns://') or starts-with(@src, 'dweb:')]" - - const pubGwURL = window.ipfsCompanionPubGwURL - - function init () { - // initial run - normalizeTree(document.body) - - // listen for future DOM changes - new MutationObserver(function (mutations) { - mutations.forEach(function (mutation) { - if (mutation.type === 'childList') { - for (const addedNode of mutation.addedNodes) { - if (addedNode.nodeType === Node.ELEMENT_NODE) { - setTimeout(() => normalizeTree(addedNode), 0) - } - } - } - }) - }).observe(document.body, { - characterData: false, - childList: true, - subtree: true - }) - } - - function normalizeElement (element) { - if (element.href) { - // console.log('normalizeElement.href: ' + element.href) - element.href = normalizeAddress(element.href) - } else if (element.src) { - // console.log('normalizeElement.src: ' + element.src) - element.src = normalizeAddress(element.src) - } - } - - // replaces unhandled protocol with a regular URL at a public gateway - function normalizeAddress (addr) { - return addr - .replace(/^dweb:\//i, pubGwURL) // dweb:/ipfs/Qm → /ipfs/Qm - .replace(/^ipfs:\/\//i, `${pubGwURL}ipfs/`) // ipfs://Qm → /ipfs/Qm - .replace(/^ipns:\/\//i, `${pubGwURL}ipns/`) // ipns://Qm → /ipns/Qm - } - - function normalizeTree (root) { - const xpathResult = document.evaluate(xpath, root, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) - let i = 0 - function continuation () { - let node = null - let counter = 0 - while ((node = xpathResult.snapshotItem(i++))) { - const parent = node.parentNode - // Skip if no longer in visible DOM - if (!parent || !root.contains(node)) continue - normalizeElement(node) - if (++counter > 10) { - return setTimeout(continuation, 0) - } - } - } - window.requestAnimationFrame(continuation) - } - - init() -}(window.ipfsCompanionNormalizedUnhandledProtocols)) diff --git a/add-on/src/lib/ipfs-companion.js b/add-on/src/lib/ipfs-companion.js index 4c49cccce..43fb1ef2b 100644 --- a/add-on/src/lib/ipfs-companion.js +++ b/add-on/src/lib/ipfs-companion.js @@ -368,7 +368,7 @@ module.exports = async function init () { if (!details.url.startsWith('http')) return // skip special pages // console.info(`[ipfs-companion] onDOMContentLoaded`, details) if (state.linkify) { - console.info(`[ipfs-companion] Running linkfy experiment for ${details.url}`) + log(`running linkfy experiment on ${details.url}`) try { await browser.tabs.executeScript(details.tabId, { file: '/dist/bundles/linkifyContentScript.bundle.js', @@ -377,29 +377,7 @@ module.exports = async function init () { runAt: 'document_idle' }) } catch (error) { - console.error(`Unable to linkify DOM at '${details.url}' due to`, error) - } - } - if (state.catchUnhandledProtocols) { - // console.log(`[ipfs-companion] Normalizing links with unhandled protocols at ${tab.url}`) - // See: https://github.com/ipfs/ipfs-companion/issues/286 - try { - // pass the URL of user-preffered public gateway - await browser.tabs.executeScript(details.tabId, { - code: `window.ipfsCompanionPubGwURL = '${state.pubGwURLString}'`, - matchAboutBlank: false, - allFrames: true, - runAt: 'document_start' - }) - // inject script that normalizes `href` and `src` containing unhandled protocols - await browser.tabs.executeScript(details.tabId, { - file: '/dist/bundles/normalizeLinksContentScript.bundle.js', - matchAboutBlank: false, - allFrames: true, - runAt: 'document_end' - }) - } catch (error) { - console.error(`Unable to normalize links at '${details.url}' due to`, error) + log.error(`Unable to linkify DOM at '${details.url}' due to`, error) } } if (details.url.startsWith(state.webuiRootUrl)) { diff --git a/webpack.config.js b/webpack.config.js index 329d44387..2ada49616 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -141,8 +141,7 @@ const contentScriptsConfig = merge(commonConfig, { name: 'contentScripts', entry: { ipfsProxyContentScriptPayload: './add-on/src/contentScripts/ipfs-proxy/page.js', - linkifyContentScript: './add-on/src/contentScripts/linkifyDOM.js', - normalizeLinksContentScript: './add-on/src/contentScripts/normalizeLinksWithUnhandledProtocols.js' + linkifyContentScript: './add-on/src/contentScripts/linkifyDOM.js' } })