-
Notifications
You must be signed in to change notification settings - Fork 56
API
- clear_cache_localget
- clear_cache_remoteget
- localget
- localget_json
- remoteget
- remoteget_json
- load_css_file
- load_css_code
- get_progress_on_enabled_goals_this_week
- list_enabled_goals
- get_enabled_goals
- list_all_goals
- get_goal_info
- get_goals
- get_positive_enabled_goals
- get_positive_enabled_uncompleted_goals
- get_random_positive_goal
- get_random_uncompleted_positive_goal
- get_goal_target
- thompsonsampling
- novelty
- thompson_novelty
- list_enabled_interventions
- get_enabled_interventions
- list_custom_interventions
- list_all_interventions
- get_intervention_info
- get_interventions
- get_number_sessions_for_each_intervention
- get_time_since_intervention
- get_novelty
- ThompsonMAB
- train_multi_armed_bandit_for_goal
- get_goal_name
- get_seconds_spent_on_current_domain_today
- get_visits_to_domain_today
- get_visits_to_current_domain_today
- get_seconds_spent_on_current_domain_in_current_session
- printable_time_spent_long
- get_news_feed
- inject_into_feed
- getUrlParameters
- once_available
- once_available_fast
- once_available_multiselect
- once_body_available
- on_url_change
- on_url_change_not_from_history
- create_shadow_div
- wrap_in_shadow
- create_shadow_div_on_body
- append_to_body_shadow
- get_random_quote
- learn
- sample_times
- normalize
- predict
- displayName
- get_streak
- close_selected_tab
- close_tab_with_id
- open_url_in_new_tab
- get_selected_tab_id
- start_episode_tracker
- stop_episode_tracker
- get_num_episodes_watched
- make_notification
- notification_onclick
- close_notification
- show_toast
- is_video_playing
- GoalInfo
- GoalName
- GoalProgressInfo
- InterventionInfo
- InterventionName
const {clear_cache_localget} = require('libs_common/cacheget_utils')
Clears the cache used by localget and localget_json
const {clear_cache_remoteget} = require('libs_common/cacheget_utils')
Clears the cache used by remoteget and remoteget_json
const {localget} = require('libs_common/cacheget_utils')
Fetches a local URL and returns the content as text. This is for data that is local to the extension, ie chrome-extension URLs - for remote HTTP/HTTPS URLs, use remoteget instead. Result is cached - if you need to clear the cache, use clear_cache_localget.
Parameters
-
urlstring The URL that we should fetch
Returns Promise<string> Content of the remote URL, as text
const {localget_json} = require('libs_common/cacheget_utils')
Fetches a local URL and returns the content as JSON. This is for data that is local to the extension, ie chrome-extension URLs - for remote HTTP/HTTPS URLs, use remoteget_json instead. Result is cached - if you need to clear the cache, use clear_cache_localget.
Parameters
-
urlstring The URL that we should fetch
Returns Promise<(Object | Array)> Content of the remote URL, as parsed JSON (either an Object or Array)
const {remoteget} = require('libs_common/cacheget_utils')
Fetches a remote URL and returns the content as text. This is for external HTTP/HTTPS URLs - for data that is local to the extension, use localget instead. Result is cached - if you need to clear the cache, use clear_cache_remoteget.
Parameters
-
urlstring The URL that we should fetch
Returns Promise<string> Content of the remote URL, as text
const {remoteget_json} = require('libs_common/cacheget_utils')
Fetches a remote URL and returns the content as parsed JSON. This is for external HTTP/HTTPS URLs - for data that is local to the extension, use localget_json instead. Result is cached - if you need to clear the cache, use clear_cache_remoteget.
Parameters
-
urlstring The URL that we should fetch
Returns Promise<(Object | Array)> Content of the remote URL, as parsed JSON (either an Object or Array)
const {load_css_file} = require('libs_common/content_script_utils')
Loads a css file
Parameters
-
filenamestring name of css package or path to css file
const {load_css_code} = require('libs_common/content_script_utils')
Loads some css code
Parameters
-
css_codestring the css code to load
const {get_progress_on_enabled_goals_this_week} = require('libs_common/goal_progress')
Gets the goal progress info on each enabled goal this week.
Returns Promise<Object<string, Array<GoalProgressInfo>>> Object mapping goal names to an array of goal progress info objects, one for each of the past 7 days (index 0=today, 1=yesterday, etc)
const {list_enabled_goals} = require('libs_common/goal_utils')
Returns a list of names of enabled goals
Returns Promise<Array<GoalName>> List of enabled goal names
const {get_enabled_goals} = require('libs_common/goal_utils')
Returns a object with with names of enabled goals as keys, and whether they are enabled as values
Returns Promise<Object<GoalName, boolean>> Object with enabled goals as keys
const {list_all_goals} = require('libs_common/goal_utils')
Lists names of all available goals
Returns Promise<Array<GoalName>> List of goal names.
const {get_goal_info} = require('libs_common/goal_utils')
Gets the goal info for the specified goal name
Parameters
-
goal_nameGoalName The name of the goal
Returns Promise<GoalInfo> The goal info
const {get_goals} = require('libs_common/goal_utils')
Gets the goal info for all goals, in the form of an object mapping goal names to goal info
Returns Promise<Object<GoalName, GoalInfo>> Object mapping goal names to goal info
const {get_positive_enabled_goals} = require('libs_common/goal_utils')
Gets the goal info for all goals where is_positive set to true, in the form of an object mapping goal names to goal info
Returns Promise<Object<GoalName, GoalInfo>> Object mapping goal names to goal info
const {get_positive_enabled_uncompleted_goals} = require('libs_common/goal_utils')
Gets the goal info for all goals where is_positive set to true and that have not yet been completed
Returns Promise<Object<GoalName, GoalInfo>> Object mapping goal names to goal info
const {get_random_positive_goal} = require('libs_common/goal_utils')
Gets the goal info for a random enabled positive goal
Returns GoalInfo The goal info
const {get_random_uncompleted_positive_goal} = require('libs_common/goal_utils')
Gets the goal info for a random enabled uncompleted positive goal
Returns GoalInfo The goal info
const {get_goal_target} = require('libs_common/goal_utils')
Gets the target time spent in seconds for the specified goal
Parameters
-
goal_nameGoalName The name of the goal
Returns Promise<Number> The target in seconds
const {thompsonsampling} = require('libs_backend/intervention_selection_algorithms')
This selection algorithm recommends an intervention for each goal using the Thompson Sampling Algorithm.
Returns any List of intervention names (strings), one intervention for each user goal.
const {novelty} = require('libs_backend/intervention_selection_algorithms')
This selection algorithm ranks the interventions from lowest to highest novelty, prioritizing the least recently seen interventions over the most recently seen interventions.
Returns any List of intervention names (strings), one intervention for each user goal.
const {thompson_novelty} = require('libs_backend/intervention_selection_algorithms')
Generates thompson-novely combo algorithm given weight (% of reward that should be from sampling) Precondition: @param weight must be between 0 and 1, inclusive.
const {list_enabled_interventions} = require('libs_common/intervention_utils')
Returns a list of names of enabled interventions
Returns Promise<Array<InterventionName>> List of enabled intervention names
const {get_enabled_interventions} = require('libs_common/intervention_utils')
Returns a object with with names of enabled interventions as keys, and whether they are enabled as values
Returns Promise<Object<InterventionName, boolean>> Object with enabled interventions as keys
const {list_custom_interventions} = require('libs_common/intervention_utils')
Returns a list of names of custom interventions
Returns Promise<Array<InterventionName>> List of names of custom interventions
const {list_all_interventions} = require('libs_common/intervention_utils')
Lists all available interventions
Returns Promise<Array<InterventionName>> The list of available interventions
const {get_intervention_info} = require('libs_common/intervention_utils')
Gets the intervention info for the specified intervention name
Parameters
-
intervention_nameInterventionName The name of the intervention
Returns Promise<InterventionInfo> The intervention info
const {get_interventions} = require('libs_common/intervention_utils')
Gets the intervention info for all interventions, in the form of an object mapping intervention names to intervention info
Returns Promise<Object<InterventionName, InterventionInfo>> Object mapping intervention names to intervention info
const {get_number_sessions_for_each_intervention} = require('libs_common/intervention_utils')
Returns a dictionary with keys being the intervention, values being the number of sessions. NOTE: These interventions are not necessarily enabled by the user.
const {get_time_since_intervention} = require('libs_common/intervention_utils')
Gets the time in milliseconds since the intervention was most recently given. If this intervention corresponds to a generic intervention, then we choose the most recent intervention across the generic one. Returns -1 if the intervention has not been used yet.
const {get_novelty} = require('libs_common/intervention_utils')
Currently, novelty is just the time since the intervention was last used. TODO: Consider some other function of that time to better represent the novelty curve. [Currently assuming linear]
Parameters
-
intervention_nameslist of strings
Returns any dictionary {<intervention_name>: novelty}
const {ThompsonMAB} = require('libs_backend/multi_armed_bandit_thompson')
This algorithm recommends interventions using the Generalized Thompson Sampling Algorithm. This Thompson Sampling Algorithm draws inspiration from: Daniel J. Russo, Benjamin Van Roy, Abbas Kazerouni, Ian Osband and Zheng Wen (2018), “A Tutorial on Thompson Sampling”, Foundations and Trends in Machine Learning: Vol. 11, No. 1, pp 1–96. DOI: 10.1561/2200000070. This Thompson Sampling is designed solely for handling a multi-armbed-bandit problem with TIME observations. Currently, we will train the algorithm with all previous sessions on each instance of the extension TODO: Investigate whether this will cause a performance bottleneck and rewrite the algorithm to maintain the posterior and only train with one new instance each time.
const {train_multi_armed_bandit_for_goal} = require('libs_backend/multi_armed_bandit_thompson')
Trains predictor for choosing which intervention to use given a goal using Thompson Sampling. Each sample is the session length using an intervention.
Parameters
-
sample_coefficient, novelty_coefficient: see ThompsonMAB
Returns any A predictor for which intervention to choose.
const {get_goal_name} = require('libs_common/intervention_info')
Gets the name of the current goal
Returns string The current goal name
const {get_seconds_spent_on_current_domain_today} = require('libs_common/time_spent_utils')
Return seconds spent on current domain today
Returns integer seconds spent
const {get_visits_to_domain_today} = require('libs_common/time_spent_utils')
Return visits to the given domain today
Parameters
-
thedomain doain
Returns integer seconds spent
const {get_visits_to_current_domain_today} = require('libs_common/time_spent_utils')
Return visits to the given domain today
Returns integer seconds spent
Return seconds spent on current domain this session
Returns integer seconds spent
const {printable_time_spent_long} = require('libs_common/time_utils')
Return time spent in hours, minutes, seconds format
Parameters
-
secondsInteger seconds spent
Returns String
const {get_news_feed} = require('libs_frontend/facebook_utils')
Inject an HTMLElement into facebook news feeds
const {inject_into_feed} = require('libs_frontend/facebook_utils')
Inject an HTMLElement into facebook news feeds. Which spot it goes in the feed is calculated using position + n * spacing for n >= 0. Eg. if position = 4 and spacing = 8, it's injected as the 4th, 12th, and 20th post and so on.
Parameters
-
component_generatorHTMLElement the created div in the shadow dom -
positionHTMLElement an optional parameter of where to insert it (default 4) -
positionHTMLElement an optional parameter for how many posts to have between them (default 8)
const {getUrlParameters} = require('libs_frontend/frontend_libs')
Gets an object containing the URL parameters. Ie, for a URL http://www.example.com/path?foo=bar&baz=qux will output {'foo': 'bar', 'baz': 'qux'}
Returns Object An object with parameter names as keys, and parameter values as values
const {once_available} = require('libs_frontend/frontend_libs')
Return an element once it's available (check every 0.1 seconds)
Parameters
Returns HTMLElement
const {once_available_fast} = require('libs_frontend/frontend_libs')
Return an element once it's available (check every 0.03 seconds)
Parameters
Returns HTMLElement
const {once_available_multiselect} = require('libs_frontend/frontend_libs')
Return multiple elements once they are available (check every 0.1 seconds)
Parameters
Returns NodeList the list of elements selected
const {once_body_available} = require('libs_frontend/frontend_libs')
Return once body is available (check every 0.03 seconds)
Parameters
-
callbackfunction
const {on_url_change} = require('libs_frontend/frontend_libs')
Execute a particular function when curren url changes
Parameters
-
funcfunction function to get executed
const {on_url_change_not_from_history} = require('libs_frontend/frontend_libs')
Execute a particular function when curren url changes
Parameters
-
funcfunction function to get executed
const {create_shadow_div} = require('libs_frontend/frontend_libs')
Creates a div in the shadow dom to protect the div styling from outside CSS
Parameters
-
optionscss styling which should be applied to shadow div
Returns any the created shadow div
const {wrap_in_shadow} = require('libs_frontend/frontend_libs')
Wraps the provided element in a div under the Shadow DOM
Parameters
-
elemHTMLElement The element to add to the Shadow DOM -
optionsObject? Options for the creation of the Shadow DOM wrapper element
Returns HTMLElement The created div in the shadow dom
const {create_shadow_div_on_body} = require('libs_frontend/frontend_libs')
Creates a div in the shadow dom to protect the div styling from outside CSS
Parameters
-
optionsObject? Options(CSS styling) for the creation of the Shadow DOM wrapper element
Returns any the created shadow div
const {append_to_body_shadow} = require('libs_frontend/frontend_libs')
Wraps the provided element in a div under the Shadow DOM and appends it to the body of the document
Parameters
-
elemHTMLElement The element to add to the Shadow DOM -
optionsObject? Options for the creation of the Shadow DOM wrapper element
Returns HTMLElement The created div in the shadow dom
const {get_random_quote} = require('libs_frontend/quote_utils')
Return a randomly selected quote from a set list of quotes
Returns Object Object containing three strings: text, source, and category
const {learn} = require('libs_backend/multi_armed_bandit_thompson')
Learns this new observation and updates the posterior.
Parameters
-
arm: name of intervention. -
observation: time spent with that intervention.
const {sample_times} = require('libs_backend/multi_armed_bandit_thompson')
Returns any dictionary of {intervention_name: time}
const {normalize} = require('libs_backend/multi_armed_bandit_thompson')
Parameters
-
dictionary: {intervention_name: number}
Returns any normalized dictionary.
const {predict} = require('libs_backend/multi_armed_bandit_thompson')
Based on our posterior parameters, recommend which intervention to choose to minimize time spent.
Parameters
-
novelty: dictionary formatted like {<intervention_name>: } where novelty is the time since that intervention was used. Optional.
Returns any : the name of the intervention we recommend.
const {displayName} = require('libs_backend/multi_armed_bandit_thompson')
Instantiates Thompson Multi Armed Bandit with Prior Distribution Parameters.
Note, our observations are time, so we can assume that our observations are log-Gaussian distributed.
For each arm (intervention), we will have (mu, sigma) parameters.
We will choose our prior parameters to be
Parameters
-
arms_list: a list of intervention names. -
sampling_factor: coefficient that represents degree to which the Thompson Sampling is considered relative to novelty factor. If higher than novelty factor, sampling factor is considered more. -
novelty_factor: coefficient that represents degree to which the novelty of an intervention is considered relative to the sampling_factor for recommending an intervention.
const {get_streak} = require('libs_common/streak_utils')
Returns the length of the streak for the given goal
Parameters
goal_info
Returns Promise<Number> length of streak
const {close_selected_tab} = require('libs_common/tab_utils')
Closes the selected tab
Returns Promise<undefined> No return value, promise resolves once tab is closed
const {close_tab_with_id} = require('libs_common/tab_utils')
Closes the tab with the specified tab id
Parameters
-
tab_idnumber The id of the tab
const {open_url_in_new_tab} = require('libs_common/tab_utils')
Opens the specified URL in a new tab
Parameters
-
urlstring The URL we want to open in a new tab
const {get_selected_tab_id} = require('libs_common/tab_utils')
Gets the tab id for the selected tab
Returns Promise<number> The id of the selected tab
const {start_episode_tracker} = require('libs_frontend/netflix_utils')
If there's no running episode tracker, start one to track the number of episodes watched
const {stop_episode_tracker} = require('libs_frontend/netflix_utils')
Stop running episode tracker
const {get_num_episodes_watched} = require('libs_frontend/netflix_utils')
Return the number of episodes watched (must call start_episode_tracker first)
Returns int number of episodes wathced
const {make_notification} = require('libs_frontend/notification_utils')
Creates a notification and returns the notification id
Parameters
-
infoObject info for creating the notification
Returns Promise<number> notification id of the created notification
const {notification_onclick} = require('libs_frontend/notification_utils')
Adds a callback that should be called when the notification is clicked
Parameters
-
notification_idnumber The notification id that the callback should be attached to -
callbackfunction Callback that will be called when the notification is clicked
const {close_notification} = require('libs_frontend/notification_utils')
Closes the notification with the specified id
Parameters
-
notification_idnumber Notification id of the notification to close
const {show_toast} = require('libs_frontend/toast_utils')
Show a toast on the webpage
Parameters
-
optionsObject a string of toast text or an object of toast properties
Returns HTMLElement The created toast
const {is_video_playing} = require('libs_frontend/youtube_utils')
Returns true if the video is currently playing
Returns boolean True if the video is playing
const {GoalInfo} = require('flowtypes/goal_info')
An object that describes the goal info
Type: Object
Properties
-
namestring Name of the goal, ie facebook/spend_less_time -
domainstring Domain on which this goal operates, ie www.facebook.com -
sitenamestring Name of site on which the goal operates, ie facebook -
sitename_printablestring Human-readable name of site on which the goal operates, ie Facebook -
descriptionstring Human-readable description of the goal -
interventionsArray<InterventionName> The list of interventions that satisfy this goal
const {GoalName} = require('flowtypes/goal_name')
The goal name, ie facebook/spend_less_time
Type: string
const {GoalProgressInfo} = require('flowtypes/goal_progress_info')
An object that describes progress made on a goal
Type: Object
Properties
-
messagestring Human-readable measurement, ie 4 minutes -
progressnumber Numeric value of progress, in the units described by the units field -
rewardnumber Numeric value for the reward function, between 0 to 1 -
unitsstring Units that the value of progress is measured in - ie, minutes
const {InterventionInfo} = require('flowtypes/intervention_info')
An object that describes the intervention info
Type: Object
Properties
-
namestring Name of the intervention, ie facebook/remove_comments -
sitenamestring Name of site on which the intervention operates -
sitename_printablestring Human-readable name of site on which the intervention operates -
descriptionstring Human-readable description of the intervention -
goalsArray<string> List of goals this intervention satisfies
const {InterventionName} = require('flowtypes/intervention_name')
The intervention name, ie facebook/remove_comments
Type: string