Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Add publishers media info request API
Browse files Browse the repository at this point in the history
  • Loading branch information
diracdeltas committed Feb 13, 2018
1 parent a085fcd commit e324721
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
9 changes: 6 additions & 3 deletions app/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -192,16 +192,19 @@ 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'
}

if (process.env.NODE_ENV === 'development') {
// 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}`
}
Expand Down
26 changes: 26 additions & 0 deletions app/extensions/brave/content/scripts/requestHandler.js
Original file line number Diff line number Diff line change
@@ -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
})
})
})
30 changes: 30 additions & 0 deletions js/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}

0 comments on commit e324721

Please sign in to comment.