From 8bb9c4d7dc433e1fb4d0c7194ae4968b22d9a7c0 Mon Sep 17 00:00:00 2001 From: yan Date: Mon, 12 Feb 2018 18:21:04 -0800 Subject: [PATCH] Add publishers media info request API Needed for https://github.com/brave-intl/bat-publisher/pull/11#pullrequestreview-95901965 and https://github.com/brave/browser-laptop/issues/13114 --- app/extensions.js | 9 ++++-- .../brave/content/scripts/requestHandler.js | 26 ++++++++++++++++ js/lib/request.js | 30 +++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 app/extensions/brave/content/scripts/requestHandler.js diff --git a/app/extensions.js b/app/extensions.js index 9cae84b5d07..336ce451314 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: [ @@ -198,6 +198,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' } @@ -205,9 +206,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) + }) +}