read filters
can identify an appropriate filter for a website
This commit is contained in:
parent
a55b0af151
commit
1505ca7fb2
2 changed files with 120 additions and 65 deletions
|
@ -1,25 +1,25 @@
|
||||||
/* secretriat.js
|
/* secretriat.js
|
||||||
Manage the local cache.
|
Manage the local cache.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export default class secretariat {
|
export default class secretariat {
|
||||||
static read(prefname, cloud = false) {
|
static read(prefname, cloud = false) {
|
||||||
/* Read all storeed data in the browser cache.
|
/* Read all storeed data in the browser cache.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
prefname: (string) the preference name
|
prefname: (string) the preference name
|
||||||
cloud: (bool) determine cloud reading, which is otherwise disabled
|
cloud: (bool) determine cloud reading, which is otherwise disabled
|
||||||
Returns: (Object) the preferences
|
Returns: (Object) the preferences
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Initialize the selected pref data.
|
// Initialize the selected pref data.
|
||||||
let pref_data;
|
let pref_data;
|
||||||
|
|
||||||
function pref_data_set(database) {
|
function pref_data_set(database) {
|
||||||
pref_data = database[prefname];
|
pref_data = database[prefname];
|
||||||
};
|
};
|
||||||
|
|
||||||
// Retrieve the data.
|
// Retrieve the data.
|
||||||
if (cloud) {
|
if (cloud) {
|
||||||
chrome.storage.sync.get(prefname, (items) => {
|
chrome.storage.sync.get(prefname, (items) => {
|
||||||
pref_data_set(items);
|
pref_data_set(items);
|
||||||
|
@ -33,34 +33,63 @@ export default class secretariat {
|
||||||
return(pref_data);
|
return(pref_data);
|
||||||
};
|
};
|
||||||
|
|
||||||
static rules(domain = ``) {
|
static rules(domain = window.location.href) {
|
||||||
/* Load all of the rules or a rule from a particular domain.
|
/* List the matching rule for a particular domain.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
domain: the RegEx of the domain
|
domain: the website to check, which --- by default --- is the current website
|
||||||
Returns: (dictionary) the rules
|
Returns: (dictionary) the rules
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let result;
|
let result;
|
||||||
|
|
||||||
|
// Read the filters.
|
||||||
|
let filters = read(`filters`);
|
||||||
|
if (filters) {
|
||||||
|
// Must only run when there stored value.
|
||||||
|
if (domain.trim()) {
|
||||||
|
// Function to loop through each object defined by their URL
|
||||||
|
function reference(article) {
|
||||||
|
/* Skim through each one and set a matching find.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
// Load the data.
|
section: the URL to check
|
||||||
|
*/
|
||||||
/*
|
|
||||||
if (domain) {
|
// Set the section in focus
|
||||||
|
let section = filters[article];
|
||||||
} else {
|
let qualified = false;
|
||||||
|
|
||||||
}*/
|
// Determine validity
|
||||||
|
if (section) {
|
||||||
|
// The filter must have a matching URL
|
||||||
console.log(`Rules are being loaded...`);
|
if (section[`URL`]) {
|
||||||
|
// Now it's time to test it.
|
||||||
|
qualified = (new RegExp(section[`URL`])).test(domain);
|
||||||
|
if (qualified && section[`filters`]) {
|
||||||
|
// Read that out.
|
||||||
|
result = section;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
// The keys
|
||||||
|
(Object.keys(filters)).forEach(reference);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Get everything as instructed.
|
||||||
|
result = filters;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Return the result.
|
||||||
|
return(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
static amend(website, rules) {
|
static amend(website, rules) {
|
||||||
/* Update the rules.
|
/* Update the rules.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
website: RegEx pattern of the website or the domain
|
website: RegEx pattern of the website or the domain
|
||||||
rules: the rules in JSON
|
rules: the rules in JSON
|
||||||
|
@ -68,52 +97,59 @@ export default class secretariat {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static write(prefname, data) {
|
static write(prefname, data) {
|
||||||
/* Write the data on the selected prefname.
|
/* Write the data on the selected prefname.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
prefname: the preference name
|
prefname: the preference name
|
||||||
data: the new data to be written
|
data: the new data to be written
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static forget(domain) {
|
static forget(domain) {
|
||||||
/* Dangerous: Resets all data or a domain's data.
|
/* Dangerous: Resets all data or a domain's data.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
domain: the external source of the filter
|
||||||
Returns: the user's confirmation
|
Returns: the user's confirmation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let forget_action = false;
|
let forget_action = false;
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
// Import alerts module.
|
// Import alerts module.
|
||||||
let alerts = await import(chrome.runtime.getURL("gui/scripts/alerts.js"));
|
let alerts = await import(chrome.runtime.getURL("gui/scripts/alerts.js"));
|
||||||
|
|
||||||
// Confirm the action.
|
// Confirm the action.
|
||||||
let forget_action = alerts.confirm_action();
|
let forget_action = alerts.confirm_action();
|
||||||
|
|
||||||
if (forget_action) {
|
if (forget_action) {
|
||||||
// Clear the data storage.
|
if (domain) {
|
||||||
chrome.storage.local.clear();
|
|
||||||
|
} else {
|
||||||
|
// Clear the data storage.
|
||||||
|
chrome.storage.local.clear();
|
||||||
|
chrome.storage.sync.clear();
|
||||||
|
};
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
return (forget_action);
|
return (forget_action);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export function read(prefname) {
|
export function read(prefname) {
|
||||||
return(secretariat.read(prefname));
|
return(secretariat.read(prefname));
|
||||||
};
|
};
|
||||||
|
|
||||||
export function rules(domain = ``) {
|
export function rules(domain = window.location.href) {
|
||||||
return(secretariat.rules(domain));
|
return(secretariat.rules(domain));
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
export function amend(website, rules) {
|
export function amend(website, rules) {
|
||||||
return (secretariat.amend(website, rules));
|
return (secretariat.amend(website, rules));
|
||||||
|
@ -122,4 +158,3 @@ export function amend(website, rules) {
|
||||||
export function forget(domain) {
|
export function forget(domain) {
|
||||||
return (secretariat.forget(domain));
|
return (secretariat.forget(domain));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,34 +1,54 @@
|
||||||
/* Watchman.js
|
/* Watchman.js
|
||||||
Be sensitive to changes and update the state.
|
Be sensitive to changes and update the state.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
// Import modules.
|
// Import modules.
|
||||||
let secretariat = await import(chrome.runtime.getURL("scripts/secretariat.js"));
|
let secretariat = await import(chrome.runtime.getURL("scripts/secretariat.js"));
|
||||||
let reader = await import(chrome.runtime.getURL("scripts/reader.js"));
|
let reader = await import(chrome.runtime.getURL("scripts/reader.js"));
|
||||||
|
|
||||||
class watchman {
|
class watchman {
|
||||||
static observe(URL = window.location.href) {
|
static observe(URL = window.location.href) {
|
||||||
/* Check the current URL.
|
/* Check the current URL.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
URL: the page URL; default value is the current webpage
|
URL: the page URL; default value is the current webpage
|
||||||
Returns: (boolean) status
|
Returns: (dictionary) the filter to follow
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Create the variable to determine the corresponding key.
|
// Create the variable to determine the corresponding key.
|
||||||
let key;
|
let activity = false;
|
||||||
|
let filters = secretariat.rules(URL);
|
||||||
|
|
||||||
secretariat.rules(URL);
|
// Check if the filters exist.
|
||||||
|
activity = (filters);
|
||||||
|
|
||||||
return (key);
|
return (activity);
|
||||||
};
|
};
|
||||||
|
|
||||||
static act() {
|
static act(filters) {
|
||||||
|
/* Act on the page.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
filters: the filter to work with
|
||||||
|
Returns: the state
|
||||||
|
*/
|
||||||
|
|
||||||
|
console.log("ShopAI works here! Click on the button in the toolbar or website to start.");
|
||||||
// TODO
|
// TODO
|
||||||
};
|
}
|
||||||
};
|
|
||||||
|
static job() {
|
||||||
watchman.observe();
|
/* The main action. */
|
||||||
watchman.act();
|
let job_task = watchman.observe();
|
||||||
|
if (job_task) {
|
||||||
|
watchman.act(job_task);
|
||||||
|
} else {
|
||||||
|
console.log("ShopAI doesn't work here (yet). Expecting something? Try checking your filters. If you know what you're doing, feel free to create a filter yourself.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
watchman.job();
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue