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

Commit

Permalink
Removes two list exclusion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Aug 2, 2018
1 parent ee75ce0 commit 03e2b95
Show file tree
Hide file tree
Showing 4 changed files with 523 additions and 1,284 deletions.
51 changes: 2 additions & 49 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
const format = require('date-fns/format')
const Immutable = require('immutable')
const electron = require('electron')
const acorn = require('acorn')
const ipc = electron.ipcMain
const session = electron.session
const path = require('path')
Expand Down Expand Up @@ -69,8 +68,7 @@ const _internal = {
verboseP: process.env.LEDGER_VERBOSE || false,
debugP: process.env.LEDGER_DEBUG || false,
ruleset: {
raw: [],
cooked: []
raw: []
}
}
let userAgent = ''
Expand Down Expand Up @@ -153,27 +151,8 @@ if (ipc) {
return
}

let ctx = urlParse(location, true)
ctx.TLD = tldjs.getPublicSuffix(ctx.host)
if (!ctx.TLD) {
if (_internal.verboseP) console.log('\nno TLD for:' + ctx.host)
event.returnValue = {}
return
}

ctx = underscore.mapObject(ctx, function (value) {
if (!underscore.isFunction(value)) return value
})
ctx.URL = location
ctx.SLD = tldjs.getDomain(ctx.host)
ctx.RLD = tldjs.getSubdomain(ctx.host)
ctx.QLD = ctx.RLD ? underscore.last(ctx.RLD.split('.')) : ''

if (!event.sender.isDestroyed()) {
event.sender.send(messages.LEDGER_PUBLISHER_RESPONSE + '-' + location, {
context: ctx,
rules: _internal.ruleset.cooked
})
event.sender.send(messages.LEDGER_PUBLISHER_RESPONSE + '-' + location)
}
})
}
Expand Down Expand Up @@ -2251,7 +2230,6 @@ const initialize = (state, paymentsEnabled) => {
if (typeof ledgerPublisher.ruleset === 'function') ledgerPublisher.ruleset = ledgerPublisher.ruleset()

_internal.ruleset.raw = ledgerPublisher.ruleset
_internal.ruleset.cooked = cookRules(ledgerPublisher.ruleset)

try {
if (!fs) fs = require('fs')
Expand Down Expand Up @@ -2292,31 +2270,6 @@ const initialize = (state, paymentsEnabled) => {
}
}

const cookRules = (rules) => {
let cooked = []
rules.forEach((rule) => {
let entry = {condition: acorn.parse(rule.condition)}
if (rule.dom) {
if (rule.dom.publisher) {
entry.publisher = {
selector: rule.dom.publisher.nodeSelector,
consequent: acorn.parse(rule.dom.publisher.consequent)
}
}
if (rule.dom.faviconURL) {
entry.faviconURL = {
selector: rule.dom.faviconURL.nodeSelector,
consequent: acorn.parse(rule.dom.faviconURL.consequent)
}
}
}
if (!entry.publisher) entry.consequent = rule.consequent ? acorn.parse(rule.consequent) : rule.consequent
cooked.push(entry)
})

return cooked
}

const schedulePromoRefFetch = () => {
if (promoRefFetchTimeoutId) {
clearTimeout(promoRefFetchTimeoutId)
Expand Down
177 changes: 5 additions & 172 deletions app/extensions/brave/content/scripts/pageInformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,127 +6,6 @@
/* jshint esversion: 6 */

(function () { try {
var resolve = (tree, context) => {
var node = tree.body ? tree.body[0] : tree

var traverse = (expr, computed) => {
var args, op1, op2, value

switch (expr.type) {
case 'ExpressionStatement':
return traverse(expr.expression)

case 'ArrayExpression':
value = []
expr.elements.forEach((element) => { value.push(traverse(element)) })
return value

case 'BinaryExpression':
op1 = traverse(expr.left)
op2 = traverse(expr.right)
switch (expr.operator) {
case '==': return (op1 === op2)
case '===': return (op1 === op2)
case '!=': return (op1 !== op2)
case '!==': return (op1 !== op2)
case '<': return (op1 < op2)
case '<=': return (op1 <= op2)
case '>': return (op1 > op2)
case '>=': return (op1 >= op2)
case '<<': return (op1 << op2)
case '>>': return (op1 >> op2)
case '>>>': return (op1 >>> op2)
case '+': return (op1 + op2)
case '-': return (op1 - op2)
case '*': return (op1 * op2)
case '/': return (op1 < op2)
case '%': return (op1 % op2)
case '|': return (op1 | op2)
case '^': return (op1 ^ op2)
case '&': return (op1 & op2)
case 'in': return (op1 in op2)

default:
throw new Error('unsupported binary operator: ' + expr.operator)
}
break

case 'CallExpression':
op1 = expr.callee
if (op1.type !== 'MemberExpression') throw new Error('unexpected callee: ' + op1.type)
value = traverse(op1.object, op1.computed)
args = []
expr['arguments'].forEach((argument) => { args.push(traverse(argument)) })
return value[traverse(op1.property, true)].apply(value, args)

case 'NewExpression':
op1 = expr.callee
if (op1.type !== 'Identifier') throw new Error('unexpected callee: ' + op1.type)
if (op1.name !== 'Set') throw new Error('only new Set(...) is allowed, not new ' + op1.name)
args = []
expr['arguments'].forEach((argument) => { args.push(traverse(argument)) })
if (args.length > 1) throw new Error('Set(...) takes at most one argument')
return new Set(args[0])

case 'ConditionalExpression':
return (traverse(expr.test) ? traverse(expr.consequent) : traverse(expr.alternate))

case 'LogicalExpression':
op1 = traverse(expr.left)
op2 = traverse(expr.right)
switch (expr.operator) {
case '&&': return (op1 && op2)
case '||': return (op1 || op2)

default:
throw new Error('unsupported logical operator: ' + expr.operator)
}
break

case 'MemberExpression':
value = traverse(expr.object, expr.computed)
return value[traverse(expr.property)]

case 'UnaryExpression':
if (!expr.prefix) throw new Error('unsupported unary operator suffix: ' + expr.operator)

op1 = traverse(expr.argument)
switch (expr.operator) {
case '-': return (-op1)
case '+': return (+op1)
case '!': return (!op1)
case '~': return (~op1)
case 'typeof': return (typeof op1)

default:
throw new Error('unsupported unary operator: ' + expr.operator)
}
break

case 'Identifier':
value = computed ? expr.name : context[expr.name]
if (value !== 'eval') return value
throw new Error('eval not allowed in expression')

case 'Literal':
return (expr.regex ? new RegExp(expr.regex.pattern) : expr.value)

default:
throw new Error('unsupported evaluation type: ' + expr.type)
}
}

if ((!node) || (node.type !== 'ExpressionStatement')) throw new Error('invalid expression')

return traverse(node.expression)
}

if ((typeof module !== 'undefined') && (typeof exports !== 'undefined')) {
module.exports = { resolve: resolve }

return
}

if (window.top !== window.self) return

// Don't allow ledger to run in incognito
Expand All @@ -142,61 +21,15 @@

var location = document.location.href
// called from ledger.js with variable LEDGER_PUBLISHER_RESPONSE
chrome.ipcRenderer.once('ledger-publisher-response-' + location, (e, pubinfo) => {
if (!pubinfo || !pubinfo.context || !pubinfo.rules) {
return console.log('no pubinfo available')
}

var context = pubinfo.context
var rules = pubinfo.rules

var i, publisher, rule
for (i = 0; i < rules.length; i++) {
rule = rules[i]

try {
if (!resolve(rule.condition, context)) continue
} catch (ex) {
console.error('error resolving rule at position #' + i + '\n', ex)
continue
}

if (rule.publisher) {
context.node = document.body.querySelector(rule.publisher.selector)
publisher = resolve(rule.publisher.consequent, context)
} else {
delete context.node
publisher = rule.consequent ? resolve(rule.consequent, context) : rule.consequent
}
if (publisher === '') continue

if (typeof publisher !== 'string') return console.log('NOT a string')

publisher.replace(new RegExp('^./+|./+$', 'g'), '')

results.publisher = publisher
if (rule.faviconURL) {
context.node = document.body.querySelector(rule.faviconURL.selector)
results.faviconURL = resolve(rule.faviconURL.consequent, context)
}
break
}

if (results.faviconURL) {
var prefix = (results.faviconURL.indexOf('//') === 0) ? document.location.protocol
: (results.faviconURL.indexOf('/') === 0) ? document.location.protocol + '//' + document.location.host
: (results.faviconURL.indexOf(':') === -1) ? document.location.protocol + '//' + document.location.host + '/'
: null
if (prefix) results.faviconURL = prefix + results.faviconURL
}

results.url = window.location.href
chrome.ipcRenderer.once('ledger-publisher-response-' + location, () => {
const url = window.location.href
results.url = url
chrome.ipcRenderer.send('dispatch-action', JSON.stringify([{
location: window.location.href,
location: url,
actionType: 'event-set-page-info',
pageInfo: results
}]))
})
var pubinfo = chrome.ipcRenderer.send('ledger-publisher', location)
chrome.ipcRenderer.send('ledger-publisher', location)

} catch (ex) { console.log(ex.toString() + '\n' + ex.stack) } })()
Loading

0 comments on commit 03e2b95

Please sign in to comment.