-
Notifications
You must be signed in to change notification settings - Fork 879
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Snuupy
committed
Aug 10, 2018
1 parent
3395adb
commit b914102
Showing
36 changed files
with
2,137 additions
and
744 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* 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/. */ | ||
|
||
import * as types from '../constants/cosmeticFilterTypes' | ||
import * as actions from '../types/actions/cosmeticFilterActions' | ||
|
||
export const siteCosmeticFilterAdded: actions.SiteCosmeticFilterAdded = (origin: string, cssfilter: string) => { | ||
return { | ||
type: types.SITE_COSMETIC_FILTER_ADDED, | ||
origin, | ||
cssfilter | ||
} | ||
} | ||
|
||
export const siteCosmeticFilterRemoved: actions.SiteCosmeticFilterRemoved = (origin: string) => { | ||
return { | ||
type: types.SITE_COSMETIC_FILTER_REMOVED, | ||
origin | ||
} | ||
} | ||
|
||
export const allCosmeticFiltersRemoved: actions.AllCosmeticFiltersRemoved = () => { | ||
return { | ||
type: types.ALL_COSMETIC_FILTERS_REMOVED | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* 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/. */ | ||
|
||
import { bindActionCreators } from 'redux' | ||
import store from '../store' | ||
import * as cosmeticFilterActions from '../../actions/cosmeticFilterActions' | ||
export default bindActionCreators(cosmeticFilterActions, store.dispatch) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export const addSiteCosmeticFilter = async (origin: string, cssfilter: string) => { | ||
chrome.storage.local.get('cosmeticFilterList', (storeData = {}) => { | ||
let storeList = Object.assign({}, storeData.cosmeticFilterList) | ||
if (storeList[origin] === undefined || storeList[origin].length === 0) { // nothing in filter list for origin | ||
storeList[origin] = [cssfilter] | ||
} else { // add entry | ||
storeList[origin].push(cssfilter) | ||
} | ||
chrome.storage.local.set({ 'cosmeticFilterList': storeList }) | ||
}) | ||
} | ||
|
||
export const removeSiteFilter = (origin: string) => { | ||
chrome.storage.local.get('cosmeticFilterList', (storeData = {}) => { | ||
let storeList = Object.assign({}, storeData.cosmeticFilterList) | ||
delete storeList[origin] | ||
chrome.storage.local.set({ 'cosmeticFilterList': storeList }) | ||
}) | ||
} | ||
|
||
export const applySiteFilters = (hostname: string) => { | ||
chrome.storage.local.get('cosmeticFilterList', (storeData = {}) => { | ||
if (storeData.cosmeticFilterList[hostname] !== undefined) { | ||
storeData.cosmeticFilterList[hostname].map((rule: string) => { | ||
console.log('applying rule', rule) | ||
chrome.tabs.insertCSS({ | ||
code: `${rule} {display: none;}` | ||
}) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
export const removeAllFilters = () => { | ||
chrome.storage.local.set({ 'cosmeticFilterList': {} }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import cosmeticFilterActions from '../actions/cosmeticFilterActions' | ||
|
||
let rule = { | ||
host: '', | ||
selector: '' | ||
} | ||
|
||
// add context menu | ||
chrome.runtime.onInstalled.addListener(function () { | ||
// parent menu | ||
chrome.contextMenus.create({ | ||
title: 'Brave', | ||
id: 'brave', | ||
contexts: ['all'] | ||
}) | ||
// block ad child menu | ||
chrome.contextMenus.create({ | ||
title: 'Block element via selector', | ||
id: 'addBlockElement', | ||
parentId: 'brave', | ||
contexts: ['all'] | ||
}) | ||
chrome.contextMenus.create({ | ||
title: 'Clear CSS rules for this site', | ||
id: 'resetSiteFilterSettings', | ||
parentId: 'brave', | ||
contexts: ['all'] | ||
}) | ||
chrome.contextMenus.create({ | ||
title: 'Clear CSS rules for all sites', | ||
id: 'resetAllFilterSettings', | ||
parentId: 'brave', | ||
contexts: ['all'] | ||
}) | ||
}) | ||
|
||
// contextMenu listener - when triggered, grab latest selector | ||
chrome.contextMenus.onClicked.addListener(function (info, tab) { | ||
switch (info.menuItemId) { | ||
case 'addBlockElement': | ||
{ | ||
rule.selector = window.prompt('CSS selector to block: ', `${rule.selector}`) || '' | ||
chrome.tabs.insertCSS({ | ||
code: `${rule.selector} {display: none;}` | ||
}) | ||
cosmeticFilterActions.siteCosmeticFilterAdded(rule.host, rule.selector) | ||
break | ||
} | ||
case 'resetSiteFilterSettings': | ||
{ | ||
cosmeticFilterActions.siteCosmeticFilterRemoved(rule.host) | ||
break | ||
} | ||
case 'resetAllFilterSettings': | ||
{ | ||
cosmeticFilterActions.allCosmeticFiltersRemoved() | ||
break | ||
} | ||
default: { | ||
console.warn('[cosmeticFilterEvents] invalid context menu option: ${info.menuItemId}') | ||
} | ||
} | ||
}) | ||
|
||
// content script listener for right click DOM selection event | ||
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { | ||
rule.host = msg.baseURI | ||
rule.selector = msg.selector | ||
sendResponse(rule) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// /* 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/. */ | ||
|
||
import * as shieldsPanelTypes from '../../constants/shieldsPanelTypes' | ||
import * as windowTypes from '../../constants/windowTypes' | ||
import * as tabTypes from '../../constants/tabTypes' | ||
import * as webNavigationTypes from '../../constants/webNavigationTypes' | ||
import { | ||
setAllowBraveShields, | ||
requestShieldPanelData | ||
} from '../api/shieldsAPI' | ||
import { reloadTab } from '../api/tabsAPI' | ||
import * as shieldsPanelState from '../../state/shieldsPanelState' | ||
import { State, Tab } from '../../types/state/shieldsPannelState' | ||
import { Actions } from '../../types/actions/index' | ||
import * as cosmeticFilterTypes from '../../constants/cosmeticFilterTypes' | ||
import { | ||
removeSiteFilter, | ||
addSiteCosmeticFilter, | ||
applySiteFilters, | ||
removeAllFilters | ||
} from '../api/cosmeticFilterAPI' | ||
|
||
const focusedWindowChanged = (state: State, windowId: number): State => { | ||
if (windowId !== -1) { | ||
state = shieldsPanelState.updateFocusedWindow(state, windowId) | ||
if (shieldsPanelState.getActiveTabId(state)) { | ||
requestShieldPanelData(shieldsPanelState.getActiveTabId(state)) | ||
} else { | ||
console.warn('no tab id so cannot request shield data from window focus change!') | ||
} | ||
} | ||
return state | ||
} | ||
|
||
const updateActiveTab = (state: State, windowId: number, tabId: number): State => { | ||
requestShieldPanelData(tabId) | ||
return shieldsPanelState.updateActiveTab(state, windowId, tabId) | ||
} | ||
|
||
export default function cosmeticFilterReducer (state: State = { | ||
tabs: {}, | ||
windows: {}, | ||
currentWindowId: -1 }, | ||
action: Actions) { | ||
switch (action.type) { | ||
case webNavigationTypes.ON_BEFORE_NAVIGATION: | ||
{ | ||
if (action.isMainFrame) { | ||
state = shieldsPanelState.resetBlockingStats(state, action.tabId) | ||
state = shieldsPanelState.resetNoScriptInfo(state, action.tabId, new window.URL(action.url).origin) | ||
} | ||
break | ||
} | ||
case webNavigationTypes.ON_COMMITTED: | ||
{ | ||
const tabData: Tab = shieldsPanelState.getActiveTabData(state) | ||
applySiteFilters(tabData.hostname) | ||
break | ||
} | ||
case windowTypes.WINDOW_REMOVED: | ||
{ | ||
state = shieldsPanelState.removeWindowInfo(state, action.windowId) | ||
break | ||
} | ||
case windowTypes.WINDOW_CREATED: | ||
{ | ||
if (action.window.focused || Object.keys(state.windows).length === 0) { | ||
state = focusedWindowChanged(state, action.window.id) | ||
} | ||
break | ||
} | ||
case windowTypes.WINDOW_FOCUS_CHANGED: | ||
{ | ||
state = focusedWindowChanged(state, action.windowId) | ||
break | ||
} | ||
case tabTypes.ACTIVE_TAB_CHANGED: | ||
{ | ||
const windowId: number = action.windowId | ||
const tabId: number = action.tabId | ||
state = updateActiveTab(state, windowId, tabId) | ||
break | ||
} | ||
case tabTypes.TAB_DATA_CHANGED: | ||
{ | ||
const tab: chrome.tabs.Tab = action.tab | ||
if (tab.active && tab.id) { | ||
state = updateActiveTab(state, tab.windowId, tab.id) | ||
} | ||
break | ||
} | ||
case tabTypes.TAB_CREATED: | ||
{ | ||
const tab: chrome.tabs.Tab = action.tab | ||
if (!tab) { | ||
break | ||
} | ||
|
||
if (tab.active && tab.id) { | ||
state = updateActiveTab(state, tab.windowId, tab.id) | ||
} | ||
break | ||
} | ||
case shieldsPanelTypes.SHIELDS_PANEL_DATA_UPDATED: | ||
{ | ||
state = shieldsPanelState.updateTabShieldsData(state, action.details.id, action.details) | ||
break | ||
} | ||
case shieldsPanelTypes.SHIELDS_TOGGLED: | ||
{ | ||
const tabId: number = shieldsPanelState.getActiveTabId(state) | ||
const tabData: Tab = shieldsPanelState.getActiveTabData(state) | ||
if (!tabData) { | ||
break | ||
} | ||
setAllowBraveShields(tabData.origin, action.setting) | ||
.then(() => { | ||
reloadTab(tabId, true).catch((e) => { | ||
console.error('Tab reload was not successful', e) | ||
}) | ||
requestShieldPanelData(shieldsPanelState.getActiveTabId(state)) | ||
}) | ||
.catch((e: any) => { | ||
console.error('Could not set shields', e) | ||
}) | ||
state = shieldsPanelState | ||
.updateTabShieldsData(state, tabId, { braveShields: action.setting }) | ||
break | ||
} | ||
case cosmeticFilterTypes.SITE_COSMETIC_FILTER_REMOVED: | ||
{ | ||
let url = action.origin | ||
removeSiteFilter(url) | ||
break | ||
} | ||
case cosmeticFilterTypes.ALL_COSMETIC_FILTERS_REMOVED: | ||
{ | ||
removeAllFilters() | ||
break | ||
} | ||
case cosmeticFilterTypes.SITE_COSMETIC_FILTER_ADDED: | ||
{ | ||
addSiteCosmeticFilter(action.origin, action.cssfilter) | ||
.catch((e) => { | ||
console.error('Could not add filter:', e) | ||
}) | ||
break | ||
} | ||
} | ||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* 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 SITE_COSMETIC_FILTER_REMOVED = 'SITE_COSMETIC_FILTER_REMOVED' | ||
export const SITE_COSMETIC_FILTER_ADDED = 'SITE_COSMETIC_FILTER_ADDED' | ||
export const LOGGED_STORAGE = 'LOGGED_STORAGE' | ||
export const ALL_COSMETIC_FILTERS_REMOVED = 'ALL_COSMETIC_FILTERS_REMOVED' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const unique = require('unique-selector').default | ||
|
||
function getCurrentURL () { | ||
return window.location.hostname | ||
} | ||
|
||
document.addEventListener('contextmenu', (event) => { | ||
let selector = unique(event.target) // this has to be done here, events can't be passed through the messaging API | ||
let baseURI = getCurrentURL() | ||
console.log(selector, baseURI) | ||
chrome.runtime.sendMessage({ | ||
selector: selector, | ||
baseURI: baseURI | ||
}) | ||
}, true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.