From 09558d99141fc3980fb12af55fd6a8ec7fd8158a Mon Sep 17 00:00:00 2001
From: Alan Shaw
Date: Wed, 15 Nov 2017 23:01:58 +0000
Subject: [PATCH 01/13] adds browserify
---
.gitignore | 2 +-
add-on/manifest.json | 6 +-
add-on/src/background/background.html | 7 +-
add-on/src/background/background.js | 3 +-
add-on/src/lib/data-i18n.js | 26 +-
add-on/src/lib/ipfs-companion.js | 50 +-
add-on/src/lib/npm/.gitignore | 7 -
add-on/src/lib/option-defaults.js | 2 +-
add-on/src/options/options.html | 4 +-
add-on/src/options/options.js | 7 +-
add-on/src/popup/browser-action.html | 3 +-
add-on/src/popup/browser-action.js | 7 +-
add-on/src/popup/quick-upload.html | 4 +-
add-on/src/popup/quick-upload.js | 9 +-
package-lock.json | 11786 ++++++++++++++++++++++++
package.json | 17 +-
16 files changed, 11874 insertions(+), 66 deletions(-)
delete mode 100644 add-on/src/lib/npm/.gitignore
create mode 100644 package-lock.json
diff --git a/.gitignore b/.gitignore
index 9e3662cfb..c7d0af5ab 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,9 +1,9 @@
vendor
node_modules
-package-lock.json
firefox
cache
build
npm-debug.log
crowdin.yml
.*~
+add-on/dist
diff --git a/add-on/manifest.json b/add-on/manifest.json
index 8f0585a32..03eea5cf3 100644
--- a/add-on/manifest.json
+++ b/add-on/manifest.json
@@ -35,7 +35,7 @@
],
"background": {
- "page": "src/background/background.html"
+ "page": "dist/background/background.html"
},
"browser_action": {
@@ -46,12 +46,12 @@
"128": "icons/png/ipfs-logo-off_128.png"
},
"default_title": "__MSG_browserAction_title__",
- "default_popup": "src/popup/browser-action.html"
+ "default_popup": "dist/popup/browser-action.html"
},
"options_ui": {
"browser_style": false,
- "page": "src/options/options.html"
+ "page": "dist/options/options.html"
},
"web_accessible_resources": [
diff --git a/add-on/src/background/background.html b/add-on/src/background/background.html
index 27d092269..53a71c3f0 100644
--- a/add-on/src/background/background.html
+++ b/add-on/src/background/background.html
@@ -1,9 +1,4 @@
-
-
-
-
-
-
+
diff --git a/add-on/src/background/background.js b/add-on/src/background/background.js
index 2e280f49e..b99482ec7 100644
--- a/add-on/src/background/background.js
+++ b/add-on/src/background/background.js
@@ -1,6 +1,7 @@
'use strict'
/* eslint-env browser, webextensions */
-/* global init */
+
+const init = require('../lib/ipfs-companion')
// init add-on after all libs are loaded
document.addEventListener('DOMContentLoaded', init)
diff --git a/add-on/src/lib/data-i18n.js b/add-on/src/lib/data-i18n.js
index 32fab4f41..d0ab6e3b8 100644
--- a/add-on/src/lib/data-i18n.js
+++ b/add-on/src/lib/data-i18n.js
@@ -1,22 +1,26 @@
'use strict'
/* eslint-env browser, webextensions */
+const browser = require('webextension-polyfill')
+
function safeTranslation (key) {
const translation = browser.i18n.getMessage(key)
return translation || '[i18n: ' + key + ']'
}
-// Search for items with data-i18n attribute and replace them with values from current locale
-const items = document.querySelectorAll('[data-i18n]')
-for (let item of items) {
- const key = item.getAttribute('data-i18n')
- if (key) {
- const translation = safeTranslation(key)
- if (typeof item.value !== 'undefined' && item.value === 'i18n') {
- // things like inputs can trigger translation with value equal "i18n"
- item.value = translation
- } else {
- item.innerText = translation
+module.exports = function () {
+ // Search for items with data-i18n attribute and replace them with values from current locale
+ const items = document.querySelectorAll('[data-i18n]')
+ for (let item of items) {
+ const key = item.getAttribute('data-i18n')
+ if (key) {
+ const translation = safeTranslation(key)
+ if (typeof item.value !== 'undefined' && item.value === 'i18n') {
+ // things like inputs can trigger translation with value equal "i18n"
+ item.value = translation
+ } else {
+ item.innerText = translation
+ }
}
}
}
diff --git a/add-on/src/lib/ipfs-companion.js b/add-on/src/lib/ipfs-companion.js
index e2e34dff6..1369536fb 100644
--- a/add-on/src/lib/ipfs-companion.js
+++ b/add-on/src/lib/ipfs-companion.js
@@ -1,18 +1,22 @@
'use strict'
/* eslint-env browser, webextensions */
-/* global optionDefaults */
+
+const browser = require('webextension-polyfill')
+const optionDefaults = require('./option-defaults')
+const { LRUMap } = require('lru_map')
+const IsIpfs = require('is-ipfs')
+const IpfsApi = require('ipfs-api')
// INIT
// ===================================================================
var ipfs // ipfs-api instance
-var state = {} // avoid redundant API reads by utilizing local cache of various states
+var state = window.state = {} // avoid redundant API reads by utilizing local cache of various states
// init happens on addon load in background/background.js
-// eslint-disable-next-line no-unused-vars
-async function init () {
+module.exports = async function init () {
try {
const options = await browser.storage.local.get(optionDefaults)
- ipfs = initIpfsApi(options.ipfsApiUrl)
+ ipfs = window.ipfs = initIpfsApi(options.ipfsApiUrl)
initStates(options)
registerListeners()
setApiStatusUpdateInterval(options.ipfsApiPollMs)
@@ -25,7 +29,7 @@ async function init () {
function initIpfsApi (ipfsApiUrl) {
const url = new URL(ipfsApiUrl)
- return window.IpfsApi({host: url.hostname, port: url.port, procotol: url.protocol})
+ return IpfsApi({host: url.hostname, port: url.port, procotol: url.protocol})
}
function initStates (options) {
@@ -44,7 +48,7 @@ function initStates (options) {
state.preloadAtPublicGateway = options.preloadAtPublicGateway
state.catchUnhandledProtocols = options.catchUnhandledProtocols
state.displayNotifications = options.displayNotifications
- state.dnslinkCache = /* global LRUMap */ new LRUMap(1000)
+ state.dnslinkCache = new LRUMap(1000)
}
function registerListeners () {
@@ -65,11 +69,11 @@ function publicIpfsOrIpnsResource (url) {
// first, exclude gateway and api, otherwise we have infinite loop
if (!url.startsWith(state.gwURLString) && !url.startsWith(state.apiURLString)) {
// /ipfs/ is easy to validate, we just check if CID is correct and return if true
- if (window.IsIpfs.ipfsUrl(url)) {
+ if (IsIpfs.ipfsUrl(url)) {
return true
}
// /ipns/ requires multiple stages/branches, as it can be FQDN with dnslink or CID
- if (window.IsIpfs.ipnsUrl(url) && validIpnsPath(new URL(url).pathname)) {
+ if (IsIpfs.ipnsUrl(url) && validIpnsPath(new URL(url).pathname)) {
return true
}
}
@@ -78,12 +82,12 @@ function publicIpfsOrIpnsResource (url) {
}
function validIpnsPath (path) {
- if (window.IsIpfs.ipnsPath(path)) {
+ if (IsIpfs.ipnsPath(path)) {
// we may have false-positives here, so we do additional checks below
const ipnsRoot = path.match(/^\/ipns\/([^/]+)/)[1]
// console.log('==> IPNS root', ipnsRoot)
// first check if root is a regular CID
- if (window.IsIpfs.cid(ipnsRoot)) {
+ if (IsIpfs.cid(ipnsRoot)) {
// console.log('==> IPNS is a valid CID', ipnsRoot)
return true
}
@@ -96,7 +100,7 @@ function validIpnsPath (path) {
}
function validIpfsOrIpnsPath (path) {
- return window.IsIpfs.ipfsPath(path) || validIpnsPath(path)
+ return IsIpfs.ipfsPath(path) || validIpnsPath(path)
}
function redirectToCustomGateway (requestUrl) {
@@ -187,7 +191,7 @@ function normalizedWebPlusRequest (request) {
path = path.replace(/^\/web\+dweb:\//i, '/') // web+dweb:/ipfs/Qm → /ipfs/Qm
path = path.replace(/^\/web\+ipfs:\/\//i, '/ipfs/') // web+ipfs://Qm → /ipfs/Qm
path = path.replace(/^\/web\+ipns:\/\//i, '/ipns/') // web+ipns://Qm → /ipns/Qm
- if (oldPath !== path && window.IsIpfs.path(path)) {
+ if (oldPath !== path && IsIpfs.path(path)) {
return { redirectUrl: urlAtPublicGw(path) }
}
return null
@@ -207,14 +211,14 @@ function unhandledIpfsPath (requestUrl) {
if (unhandled && unhandled.length > 1) {
const unhandledProtocol = decodeURIComponent(unhandled[1])
const unhandledPath = `/${decodeURIComponent(unhandled[2])}`
- return window.IsIpfs.path(unhandledPath) ? unhandledPath : `/${unhandledProtocol}${unhandledPath}`
+ return IsIpfs.path(unhandledPath) ? unhandledPath : `/${unhandledProtocol}${unhandledPath}`
}
return null
}
function normalizedUnhandledIpfsProtocol (request) {
const path = unhandledIpfsPath(request.url)
- if (window.IsIpfs.path(path)) {
+ if (IsIpfs.path(path)) {
// replace search query with fake request to the public gateway
// (will be redirected later, if needed)
return { redirectUrl: urlAtPublicGw(path) }
@@ -234,7 +238,7 @@ function isDnslookupSafeForURL (requestUrl) {
// skip URLs that could produce infinite recursion or weird loops
return isDnslookupPossible() &&
requestUrl.startsWith('http') &&
- !window.IsIpfs.url(requestUrl) &&
+ !IsIpfs.url(requestUrl) &&
!requestUrl.startsWith(state.apiURLString) &&
!requestUrl.startsWith(state.gwURLString)
}
@@ -295,7 +299,7 @@ function readDnslinkFromTxtRecord (fqdn) {
if (xhr.status === 200) {
const dnslink = JSON.parse(xhr.responseText).Path
// console.log('readDnslinkFromTxtRecord', readDnslinkFromTxtRecord)
- if (!window.IsIpfs.path(dnslink)) {
+ if (!IsIpfs.path(dnslink)) {
throw new Error(`dnslink for '${fqdn}' is not a valid IPFS path: '${dnslink}'`)
}
return dnslink
@@ -504,6 +508,8 @@ function uploadResultHandler (err, result) {
})
}
+window.uploadResultHandler = uploadResultHandler
+
// Copying URLs
// -------------------------------------------------------------------
@@ -512,6 +518,8 @@ function safeIpfsPath (urlOrPath) {
return decodeURIComponent(urlOrPath.replace(/^.*(\/ip(f|n)s\/.+)$/, '$1'))
}
+window.safeIpfsPath = safeIpfsPath
+
async function findUrlForContext (context) {
if (context) {
if (context.linkUrl) {
@@ -539,6 +547,8 @@ async function copyCanonicalAddress (context) {
notify('notify_copiedCanonicalAddressTitle', rawIpfsAddress)
}
+window.copyCanonicalAddress = copyCanonicalAddress
+
async function copyAddressAtPublicGw (context) {
const url = await findUrlForContext(context)
const urlAtPubGw = url.replace(state.gwURLString, state.pubGwURLString)
@@ -546,6 +556,8 @@ async function copyAddressAtPublicGw (context) {
notify('notify_copiedPublicURLTitle', urlAtPubGw)
}
+window.copyAddressAtPublicGw = copyAddressAtPublicGw
+
async function copyTextToClipboard (copyText) {
const currentTab = await browser.tabs.query({active: true, currentWindow: true}).then(tabs => tabs[0])
const tabId = currentTab.id
@@ -596,7 +608,7 @@ async function updateContextMenus (changedTabId) {
// used in browser-action popup
// eslint-disable-next-line no-unused-vars
function isIpfsPageActionsContext (url) {
- return window.IsIpfs.url(url) && !url.startsWith(state.apiURLString)
+ return IsIpfs.url(url) && !url.startsWith(state.apiURLString)
}
async function onActivatedTab (activeInfo) {
@@ -831,7 +843,7 @@ function onStorageChange (changes, area) { // eslint-disable-line no-unused-vars
if (key === 'ipfsApiUrl') {
state.apiURL = new URL(change.newValue)
state.apiURLString = state.apiURL.toString()
- ipfs = initIpfsApi(state.apiURLString)
+ ipfs = window.ipfs = initIpfsApi(state.apiURLString)
apiStatusUpdate()
} else if (key === 'ipfsApiPollMs') {
setApiStatusUpdateInterval(change.newValue)
diff --git a/add-on/src/lib/npm/.gitignore b/add-on/src/lib/npm/.gitignore
deleted file mode 100644
index bba865eec..000000000
--- a/add-on/src/lib/npm/.gitignore
+++ /dev/null
@@ -1,7 +0,0 @@
-# This directory is a placeholder for 3rd party libraries
-# that should not be tracked with Git
-#
-# Ignore everything in this directory
-*
-# Except this file
-!.gitignore
diff --git a/add-on/src/lib/option-defaults.js b/add-on/src/lib/option-defaults.js
index 56063e810..ae924f2a9 100644
--- a/add-on/src/lib/option-defaults.js
+++ b/add-on/src/lib/option-defaults.js
@@ -1,7 +1,7 @@
'use strict'
/* eslint-env browser, webextensions */
-const optionDefaults = Object.freeze({ // eslint-disable-line no-unused-vars
+module.exports = Object.freeze({
publicGatewayUrl: 'https://ipfs.io',
useCustomGateway: true,
automaticMode: true,
diff --git a/add-on/src/options/options.html b/add-on/src/options/options.html
index e0eac8bbd..3723a318c 100644
--- a/add-on/src/options/options.html
+++ b/add-on/src/options/options.html
@@ -90,7 +90,6 @@
transition: box-shadow 0.3s;
}
-
-
-
+
diff --git a/add-on/src/options/options.js b/add-on/src/options/options.js
index 8a5e8d426..6773b771a 100644
--- a/add-on/src/options/options.js
+++ b/add-on/src/options/options.js
@@ -1,6 +1,11 @@
'use strict'
/* eslint-env browser, webextensions */
-/* global optionDefaults */
+
+const browser = require('webextension-polyfill')
+const optionDefaults = require('../lib/option-defaults')
+const translateDataAttrs = require('../lib/data-i18n')
+
+translateDataAttrs()
async function saveOption (name) {
const element = document.querySelector(`#${name}`)
diff --git a/add-on/src/popup/browser-action.html b/add-on/src/popup/browser-action.html
index 9eadcbacb..d9b3967bc 100644
--- a/add-on/src/popup/browser-action.html
+++ b/add-on/src/popup/browser-action.html
@@ -166,7 +166,6 @@
}
.hidden { display: none !important; }
-
@@ -235,7 +234,7 @@
-
+
diff --git a/add-on/src/popup/browser-action.js b/add-on/src/popup/browser-action.js
index 69041aaba..8542d0f99 100644
--- a/add-on/src/popup/browser-action.js
+++ b/add-on/src/popup/browser-action.js
@@ -1,6 +1,11 @@
'use strict'
/* eslint-env browser, webextensions */
+const browser = require('webextension-polyfill')
+const translateDataAttrs = require('../lib/data-i18n')
+
+translateDataAttrs()
+
const ipfsContextActions = document.getElementById('ipfs-resource-context-actions')
const pinResourceButton = document.getElementById('pin-current-ipfs-address')
const unpinResourceButton = document.getElementById('unpin-current-ipfs-address')
@@ -186,7 +191,7 @@ async function updatePageActions () {
// Global Actions
// ===================================================================
-quickUpload.onclick = () => browser.tabs.create({ url: browser.extension.getURL('src/popup/quick-upload.html') })
+quickUpload.onclick = () => browser.tabs.create({ url: browser.extension.getURL('dist/popup/quick-upload.html') })
enableRedirect.onclick = () => browser.storage.local.set({useCustomGateway: true})
.then(updateBrowserActionPopup)
diff --git a/add-on/src/popup/quick-upload.html b/add-on/src/popup/quick-upload.html
index c915e3054..1e346c2de 100644
--- a/add-on/src/popup/quick-upload.html
+++ b/add-on/src/popup/quick-upload.html
@@ -26,9 +26,7 @@
-
-
-
+