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

Commit

Permalink
template for sending config info to content scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
bridiver committed Jul 7, 2016
1 parent 3a285d6 commit 20375fb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
28 changes: 17 additions & 11 deletions app/extensions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const AppStore = require('../js/stores/appStore')
const appConfig = require('../js/constants/appConfig')
const config = require('../js/constants/config')
const { getAppUrl, getExtensionsPath, getIndexHTML } = require('../js/lib/appUrlUtil')
const { getSetting } = require('../js/settings')
Expand Down Expand Up @@ -133,27 +132,34 @@ let defaultExtensions = {
LastPass: 'hdokiejnpimakedhajhdlcegeplioahd'
}

let backgroundPage = null
let backgroundPages = {}
let onLoadedCallbacks = []

module.exports.sendToTab = (tabId, message) => {
if (backgroundPage) {
backgroundPage.send('tab-message', tabId, message, [].slice.call(arguments, 2))
module.exports.onBackgroundPageLoaded = (cb) => {
onLoadedCallbacks.push(cb)
// run on any existing background pages
for (var id in backgroundPages) {
backgroundPages[id].send('background-page-message', cb())
}
}

const runOnLoadedCallbacks = (webContents) => {
onLoadedCallbacks.forEach((cb) => {
webContents.send('background-page-message', cb())
})
}

module.exports.init = () => {
process.on('background-page-loaded', function (extensionId, backgroundPageWebContents) {
process.on('background-page-loaded', function (extensionId, webContents) {
if (extensionId === config.braveExtensionId) {
backgroundPage = backgroundPageWebContents
backgroundPage.on('dom-ready', () => {
backgroundPage.send('update-state', AppStore.getState().toJS(), appConfig)
})
backgroundPages[webContents.id] = webContents
runOnLoadedCallbacks(webContents)
}
})

process.on('background-page-destroyed', function (extensionId, backgroundPageId) {
if (extensionId === config.braveExtensionId) {
backgroundPage = null
delete backgroundPages[backgroundPageId]
}
})

Expand Down
20 changes: 14 additions & 6 deletions app/extensions/brave/brave-background.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
chrome.runtime.onConnect.addListener((port) => {
port.onMessage.addListener((msg) => {
if (!port.sender || !port.sender.tab || port.sender.id !== chrome.runtime.id)
{
let ledgerPublisherConfig = {}

chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (!sender.tab || sender.tab.incognito || sender.id !== chrome.runtime.id)
return

if (msg.type === 'action') {
chrome.ipc.send('dispatch-action', JSON.stringify(msg.action))
if (msg.type === 'ledger-publisher-config') {
sendResponse(ledgerPublisherConfig)
}
})

chrome.ipc.on('background-page-message', (evt, msg) => {
if (msg.type === 'ledger-publisher-update') {
ledgerPublisherConfig = msg.config
}
})
})
}
41 changes: 26 additions & 15 deletions app/extensions/brave/content/scripts/publisherIdentification.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
// // youtube example code
// let publisherInfo = {}
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

// // attribution value
// let node = document.querySelector("meta[name='attribution']")
// if (node) {
// publisherInfo.attribution = node.getAttributeNode("content").value
// }

// // ytid
// node = document.querySelector(".yt-user-info *[data-ytid]")
// if (node) {
// publisherInfo.ytid = node.dataset.ytid
// }
chrome.runtime.sendMessage({type: 'ledger-publisher-config'}, function(ledgerPublisherConfig) {
console.log(ledgerPublisherConfig);

// if (Object.keys(publisherInfo).length !== 0) {
// ExtensionActions.setPagePublisher(document.location.href, publisherInfo)
// }
let results = {}

let node = document.head.querySelector("link[rel='icon']")
if (!node) {
node = document.head.querySelector("link[rel='shortcut icon']")
}
if (node) {
results.favicon = node.getAttribute('href')
}

// hard-coded for now... will be dynamic in the beta (I hope!)
let href = document.location.href
if (href.hostname === 'youtube.com' && href.pathname.indexOf('/channel') === 0) {
node = document.body.querySelector("#watch7-content.watch-main-col meta[itemprop='channelId']")
if (node) results.publisher = node.getAttribute('content')
}

if (Object.keys(results).length !== 0) {
ExtensionActions.setPageInfo(href, results)
}
});

2 comments on commit 20375fb

@mrose
Copy link

@mrose mrose commented on 20375fb Jul 7, 2016 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bridiver
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, thanks! I get slack and github usernames mixed up all the time
@mrose17

Please sign in to comment.