diff --git a/app/extensions.js b/app/extensions.js index 46e72f31f41..8d962fef3ed 100644 --- a/app/extensions.js +++ b/app/extensions.js @@ -38,7 +38,7 @@ let generateBraveManifest = () => { manifest_version: 2, version: '1.0', background: { - scripts: [ 'content/scripts/idleHandler.js' ], + scripts: [ 'content/scripts/requestHandler.js', 'content/scripts/idleHandler.js' ], persistent: true }, content_scripts: [ @@ -192,6 +192,7 @@ let generateBraveManifest = () => { 'style-src': '\'self\' \'unsafe-inline\'', 'font-src': '\'self\' data:', 'img-src': '* data: file://*', + 'connect-src': 'https://www.youtube.com https://www.twitch.tv', 'frame-src': '\'self\' https://brave.com' } @@ -199,9 +200,11 @@ let generateBraveManifest = () => { // allow access to webpack dev server resources let devServer = 'localhost:' + process.env.npm_package_config_port cspDirectives['default-src'] = '\'self\' http://' + devServer - cspDirectives['connect-src'] = ['\'self\'', + cspDirectives['connect-src'] = cspDirectives['connect-src'] + [ + ' \'self\'', 'http://' + devServer, - 'ws://' + devServer].join(' ') + 'ws://' + devServer + ].join(' ') cspDirectives['style-src'] = '\'self\' \'unsafe-inline\' http://' + devServer cspDirectives['font-src'] += ` http://${devServer}` } diff --git a/app/extensions/brave/content/scripts/requestHandler.js b/app/extensions/brave/content/scripts/requestHandler.js new file mode 100644 index 00000000000..ecb8295ad34 --- /dev/null +++ b/app/extensions/brave/content/scripts/requestHandler.js @@ -0,0 +1,26 @@ +const ipc = chrome.ipcRenderer + +ipc.send('got-background-page-webcontents') + +ipc.on('fetch-publisher-info', (e, url, options) => { + let finalUrl = url + window.fetch(url, options).then((response) => { + finalUrl = response.url + return response.text() + }).then((text) => { + const parser = new DOMParser() + const html = parser.parseFromString(text, 'text/html') + ipc.send('got-publisher-info-' + url, { + error: null, + url: finalUrl, + title: html.title, + image: html.querySelector('meta[property="og:image:secure_url"],meta[property="og:image:url"],meta[property="og:image"],meta[name="twitter:image:src"],meta[name="twitter:image"]').content + }) + }).catch((err) => { + console.log('fetch error', err) + ipc.send('got-publisher-info-' + url, { + url: finalUrl, + error: err.message + }) + }) +}) diff --git a/js/lib/request.js b/js/lib/request.js index b7cc6420daa..e4a123a2852 100644 --- a/js/lib/request.js +++ b/js/lib/request.js @@ -8,8 +8,16 @@ const electron = require('electron') const session = electron.session const underscore = require('underscore') const urlParse = require('../../app/common/urlParse') +const ipc = electron.ipcMain var cachedDefaultSession = null +var backgroundPageWebContents = null + +if (ipc) { + ipc.on('got-background-page-webcontents', (e) => { + backgroundPageWebContents = e.sender + }) +} const getDefaultSession = () => { if (!cachedDefaultSession) { @@ -84,3 +92,25 @@ module.exports.requestDataFile = (url, headers, path, reject, resolve) => { }) } } + +/** + * Fetches url, title, and image for a publishers site (Youtube, Twitch, etc.) + * See + * https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch + * @param {string} url - url to fetch + * @param {Object} options - options to pass to window.fetch + * @param {Function(Object)} callback + */ +module.exports.fetchPublisherInfo = (url, options, callback) => { + if (!backgroundPageWebContents) { + callback({ + url, + error: 'Background page web contents not initialized.' + }) + return + } + backgroundPageWebContents.send('fetch-publisher-info', url, options) + ipc.once('got-publisher-info-' + url, (e, response) => { + callback(response) + }) +}