This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from raphaeleidus/saved-filters
Saved Filters
- Loading branch information
Showing
5 changed files
with
216 additions
and
15 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,4 +1,48 @@ | ||
const domain = new URL(location.href).hostname; | ||
|
||
type SavedFilter = { | ||
domain: string, | ||
filter: string, | ||
} | ||
|
||
export function saveStorage(value) { | ||
chrome.storage.sync.set({options: value}, function () { | ||
}); | ||
} | ||
|
||
export function addFilter(filter: string, done?:()=>void) { | ||
chrome.storage.sync.get(['storedFilters'], (result) => { | ||
const storedFilters = result.storedFilters as string[] || []; | ||
const filterObj = { | ||
filter, | ||
domain | ||
} | ||
let filterSet = new Set(storedFilters); | ||
filterSet.add(JSON.stringify(filterObj)); | ||
chrome.storage.sync.set({storedFilters: Array.from(filterSet)}, done) | ||
}); | ||
} | ||
|
||
export function removeFilter(filter: string, done?:()=>void) { | ||
chrome.storage.sync.get(['storedFilters'], (result) => { | ||
const storedFilters = result.storedFilters as string[] || []; | ||
let filterSet = new Set(storedFilters); | ||
const filterObj = { | ||
filter, | ||
domain | ||
} | ||
filterSet.delete(JSON.stringify(filterObj)); | ||
chrome.storage.sync.set({storedFilters: Array.from(filterSet)}, done); | ||
}); | ||
} | ||
|
||
export function getFilters(callback:(filterlist: string[]) => void) { | ||
chrome.storage.sync.get(['storedFilters'], (result) => { | ||
const filtersForDomain = (result.storedFilters as string[] || []) | ||
.map(s => JSON.parse(s) as SavedFilter) | ||
// .filter(f => f.domain === domain) | ||
// In the future this will make filters linked to the domain they were created in. | ||
const filterStrings = filtersForDomain.map(({ filter }) => filter ) | ||
callback(filterStrings); | ||
}); | ||
} |
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