Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cosmetic filtering #3121

Merged
merged 2 commits into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* 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/. */

export const addSiteCosmeticFilter = async (origin: string, cssfilter: string) => {
chrome.storage.local.get('cosmeticFilterList', (storeData = {}) => {
let storeList = Object.assign({}, storeData.cosmeticFilterList)
Expand All @@ -18,7 +22,7 @@ export const removeSiteFilter = (origin: string) => {
})
}

export const applySiteFilters = (hostname: string) => {
export const applySiteFilters = (tabId: number, hostname: string) => {
chrome.storage.local.get('cosmeticFilterList', (storeData = {}) => {
if (!storeData.cosmeticFilterList) {
if (process.env.NODE_ENV === 'shields_development') {
Expand All @@ -31,8 +35,9 @@ export const applySiteFilters = (hostname: string) => {
if (process.env.NODE_ENV === 'shields_development') {
console.log('applying rule', rule)
}
chrome.tabs.insertCSS({
code: `${rule} {display: none;}`,
chrome.tabs.insertCSS(tabId, { // https://github.com/brave/brave-browser/wiki/Cosmetic-Filtering
code: `${rule} {display: none !important;}`,
cssOrigin: 'user',
runAt: 'document_start'
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export function onSelectorReturned (response: any) {

if (rule.selector && rule.selector.length > 0) {
chrome.tabs.insertCSS({
code: `${rule.selector} {display: none;}`
code: `${rule.selector} {display: none !important;}`,
cssOrigin: 'user'
})
cosmeticFilterActions.siteCosmeticFilterAdded(rule.host, rule.selector)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
// State helpers
import * as shieldsPanelState from '../../state/shieldsPanelState'
import * as noScriptState from '../../state/noScriptState'
import { getOrigin } from '../../helpers/urlUtils'
import { getOrigin, getHostname } from '../../helpers/urlUtils'
import { areObjectsEqual } from '../../helpers/objectUtils'

const focusedWindowChanged = (state: State, windowId: number): State => {
Expand Down Expand Up @@ -70,7 +70,7 @@ export default function cosmeticFilterReducer (
state = shieldsPanelState.resetBlockingResources(state, action.tabId)
state = noScriptState.resetNoScriptInfo(state, action.tabId, getOrigin(action.url))
}
applySiteFilters(tabData.hostname)
applySiteFilters(action.tabId, getHostname(action.url))
break
}
case windowTypes.WINDOW_REMOVED: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,11 @@ describe('cosmeticFilterTestSuite', () => {
'brave.com': [filter]
}
})
cosmeticFilterAPI.applySiteFilters('brave.com')
expect(insertCSSStub.getCall(0).args[0]).toEqual({
code: `${ filter } {display: none;}`,
cosmeticFilterAPI.applySiteFilters(1, 'brave.com')
expect(insertCSSStub.getCall(0).args[0]).toEqual(1)
expect(insertCSSStub.getCall(0).args[1]).toEqual({
code: `${filter} {display: none !important;}`,
cssOrigin: 'user',
runAt: 'document_start'
})
})
Expand All @@ -218,13 +220,17 @@ describe('cosmeticFilterTestSuite', () => {
'brave.com': [filter, filter2]
}
})
cosmeticFilterAPI.applySiteFilters('brave.com')
expect(insertCSSStub.getCall(0).args[0]).toEqual({
code: `${ filter } {display: none;}`,
cosmeticFilterAPI.applySiteFilters(1, 'brave.com')
expect(insertCSSStub.getCall(0).args[0]).toEqual(1)
expect(insertCSSStub.getCall(0).args[1]).toEqual({
code: `${filter } {display: none !important;}`,
cssOrigin: 'user',
runAt: 'document_start'
})
expect(insertCSSStub.getCall(1).args[0]).toEqual({
code: `${ filter2 } {display: none;}`,
expect(insertCSSStub.getCall(1).args[0]).toEqual(1)
expect(insertCSSStub.getCall(1).args[1]).toEqual({
code: `${ filter2 } {display: none !important;}`,
cssOrigin: 'user',
runAt: 'document_start'
})

Expand All @@ -234,7 +240,7 @@ describe('cosmeticFilterTestSuite', () => {
getStorageStub.yields({
cosmeticFilterList: {}
})
cosmeticFilterAPI.applySiteFilters('brave.com')
cosmeticFilterAPI.applySiteFilters(1, 'brave.com')
expect(insertCSSStub.called).toBe(false)
})
it('doesn\'t apply filters if storage is explicitly undefined', () => {
Expand All @@ -243,7 +249,7 @@ describe('cosmeticFilterTestSuite', () => {
'brave.com': undefined
}
})
cosmeticFilterAPI.applySiteFilters('brave.com')
cosmeticFilterAPI.applySiteFilters(1, 'brave.com')
expect(insertCSSStub.called).toBe(false)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ describe('cosmeticFilterEvents events', () => {
selectorToReturn = '#test_selector'
cosmeticFilterEvents.onSelectorReturned(selectorToReturn)
let returnObj = {
'code': '#test_selector {display: none;}'
'code': '#test_selector {display: none !important;}',
'cssOrigin': 'user'
}
expect(insertCssSpy).toBeCalledWith(returnObj)
})
Expand Down