Skip to content
jacobray357 edited this page Aug 2, 2018 · 23 revisions

Table of Contents

clear_cache_localget

const {clear_cache_localget} = require('libs_common/cacheget_utils')

Clears the cache used by localget and localget_json

clear_cache_remoteget

const {clear_cache_remoteget} = require('libs_common/cacheget_utils')

Clears the cache used by remoteget and remoteget_json

localget

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

  • url string The URL that we should fetch

Returns Promise<string> Content of the remote URL, as text

localget_json

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

  • url string The URL that we should fetch

Returns Promise<(Object | Array)> Content of the remote URL, as parsed JSON (either an Object or Array)

remoteget

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

  • url string The URL that we should fetch

Returns Promise<string> Content of the remote URL, as text

remoteget_json

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

  • url string The URL that we should fetch

Returns Promise<(Object | Array)> Content of the remote URL, as parsed JSON (either an Object or Array)

load_css_file

const {load_css_file} = require('libs_common/content_script_utils')

Loads a css file

Parameters

  • filename string name of css package or path to css file

load_css_code

const {load_css_code} = require('libs_common/content_script_utils')

Loads some css code

Parameters

  • css_code string the css code to load

get_progress_on_enabled_goals_this_week

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)

list_enabled_goals

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

get_enabled_goals

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

list_all_goals

const {list_all_goals} = require('libs_common/goal_utils')

Lists names of all available goals

Returns Promise<Array<GoalName>> List of goal names.

get_goal_info

const {get_goal_info} = require('libs_common/goal_utils')

Gets the goal info for the specified goal name

Parameters

  • goal_name GoalName The name of the goal

Returns Promise<GoalInfo> The goal info

get_goals

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

get_positive_enabled_goals

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

get_positive_enabled_uncompleted_goals

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

get_random_positive_goal

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

get_random_uncompleted_positive_goal

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

get_goal_target

const {get_goal_target} = require('libs_common/goal_utils')

Gets the target time spent in seconds for the specified goal

Parameters

  • goal_name GoalName The name of the goal

Returns Promise<Number> The target in seconds

thompsonsampling

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.

novelty

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.

thompson_novelty

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.

list_enabled_interventions

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

get_enabled_interventions

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

list_custom_interventions

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

list_all_interventions

const {list_all_interventions} = require('libs_common/intervention_utils')

Lists all available interventions

Returns Promise<Array<InterventionName>> The list of available interventions

get_intervention_info

const {get_intervention_info} = require('libs_common/intervention_utils')

Gets the intervention info for the specified intervention name

Parameters

Returns Promise<InterventionInfo> The intervention info

get_interventions

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

get_number_sessions_for_each_intervention

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.

get_time_since_intervention

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.

get_novelty

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_names list of strings

Returns any dictionary {<intervention_name>: novelty}

ThompsonMAB

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.

train_multi_armed_bandit_for_goal

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.

get_goal_name

const {get_goal_name} = require('libs_common/intervention_info')

Gets the name of the current goal

Returns string The current goal name

get_seconds_spent_on_current_domain_today

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

get_visits_to_domain_today

const {get_visits_to_domain_today} = require('libs_common/time_spent_utils')

Return visits to the given domain today

Parameters

  • the domain doain

Returns integer seconds spent

get_visits_to_current_domain_today

const {get_visits_to_current_domain_today} = require('libs_common/time_spent_utils')

Return visits to the given domain today

Returns integer seconds spent

get_seconds_spent_on_current_domain_in_current_session

const {get_seconds_spent_on_current_domain_in_current_session} = require('libs_common/time_spent_utils')

Return seconds spent on current domain this session

Returns integer seconds spent

printable_time_spent_long

const {printable_time_spent_long} = require('libs_common/time_utils')

Return time spent in hours, minutes, seconds format

Parameters

  • seconds Integer seconds spent

Returns String

get_news_feed

const {get_news_feed} = require('libs_frontend/facebook_utils')

Inject an HTMLElement into facebook news feeds

inject_into_feed

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_generator HTMLElement the created div in the shadow dom
  • position HTMLElement an optional parameter of where to insert it (default 4)
  • position HTMLElement an optional parameter for how many posts to have between them (default 8)

getUrlParameters

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

once_available

const {once_available} = require('libs_frontend/frontend_libs')

Return an element once it's available (check every 0.1 seconds)

Parameters

Returns HTMLElement

once_available_fast

const {once_available_fast} = require('libs_frontend/frontend_libs')

Return an element once it's available (check every 0.03 seconds)

Parameters

Returns HTMLElement

once_available_multiselect

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

once_body_available

const {once_body_available} = require('libs_frontend/frontend_libs')

Return once body is available (check every 0.03 seconds)

Parameters

on_url_change

const {on_url_change} = require('libs_frontend/frontend_libs')

Execute a particular function when curren url changes

Parameters

  • func function function to get executed

on_url_change_not_from_history

const {on_url_change_not_from_history} = require('libs_frontend/frontend_libs')

Execute a particular function when curren url changes

Parameters

  • func function function to get executed

create_shadow_div

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

  • options css styling which should be applied to shadow div

Returns any the created shadow div

wrap_in_shadow

const {wrap_in_shadow} = require('libs_frontend/frontend_libs')

Wraps the provided element in a div under the Shadow DOM

Parameters

  • elem HTMLElement The element to add to the Shadow DOM
  • options Object? Options for the creation of the Shadow DOM wrapper element

Returns HTMLElement The created div in the shadow dom

create_shadow_div_on_body

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

  • options Object? Options(CSS styling) for the creation of the Shadow DOM wrapper element

Returns any the created shadow div

append_to_body_shadow

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

  • elem HTMLElement The element to add to the Shadow DOM
  • options Object? Options for the creation of the Shadow DOM wrapper element

Returns HTMLElement The created div in the shadow dom

get_random_quote

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

learn

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.

sample_times

const {sample_times} = require('libs_backend/multi_armed_bandit_thompson')

Returns any dictionary of {intervention_name: time}

normalize

const {normalize} = require('libs_backend/multi_armed_bandit_thompson')

Parameters

  • dictionary : {intervention_name: number}

Returns any normalized dictionary.

predict

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.

displayName

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 $$\mu_i=-1/2$$ and $$\sigma_i^2=1$$ so $$E[\theta_i]=1$$ for each intervention $$i$$. We will also assume that our sigma_tilde (Gaussian Noise) is 1. TODO: Investigate this assumption.

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.

get_streak

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

close_selected_tab

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

close_tab_with_id

const {close_tab_with_id} = require('libs_common/tab_utils')

Closes the tab with the specified tab id

Parameters

  • tab_id number The id of the tab

open_url_in_new_tab

const {open_url_in_new_tab} = require('libs_common/tab_utils')

Opens the specified URL in a new tab

Parameters

  • url string The URL we want to open in a new tab

get_selected_tab_id

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

start_episode_tracker

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

stop_episode_tracker

const {stop_episode_tracker} = require('libs_frontend/netflix_utils')

Stop running episode tracker

get_num_episodes_watched

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

make_notification

const {make_notification} = require('libs_frontend/notification_utils')

Creates a notification and returns the notification id

Parameters

  • info Object info for creating the notification
    • info.title string title for the notification
    • info.icon string path to the icon for the notification
    • info.body string body of the notification message

Returns Promise<number> notification id of the created notification

notification_onclick

const {notification_onclick} = require('libs_frontend/notification_utils')

Adds a callback that should be called when the notification is clicked

Parameters

  • notification_id number The notification id that the callback should be attached to
  • callback function Callback that will be called when the notification is clicked

close_notification

const {close_notification} = require('libs_frontend/notification_utils')

Closes the notification with the specified id

Parameters

  • notification_id number Notification id of the notification to close

show_toast

const {show_toast} = require('libs_frontend/toast_utils')

Show a toast on the webpage

Parameters

  • options Object a string of toast text or an object of toast properties
    • options.text string toast text
    • options.duration string duration of the toast
    • options.close_button string close button

Returns HTMLElement The created toast

is_video_playing

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

GoalInfo

const {GoalInfo} = require('flowtypes/goal_info')

An object that describes the goal info

Type: Object

Properties

  • name string Name of the goal, ie facebook/spend_less_time
  • domain string Domain on which this goal operates, ie www.facebook.com
  • sitename string Name of site on which the goal operates, ie facebook
  • sitename_printable string Human-readable name of site on which the goal operates, ie Facebook
  • description string Human-readable description of the goal
  • interventions Array<InterventionName> The list of interventions that satisfy this goal

GoalName

const {GoalName} = require('flowtypes/goal_name')

The goal name, ie facebook/spend_less_time

Type: string

GoalProgressInfo

const {GoalProgressInfo} = require('flowtypes/goal_progress_info')

An object that describes progress made on a goal

Type: Object

Properties

  • message string Human-readable measurement, ie 4 minutes
  • progress number Numeric value of progress, in the units described by the units field
  • reward number Numeric value for the reward function, between 0 to 1
  • units string Units that the value of progress is measured in - ie, minutes

InterventionInfo

const {InterventionInfo} = require('flowtypes/intervention_info')

An object that describes the intervention info

Type: Object

Properties

  • name string Name of the intervention, ie facebook/remove_comments
  • sitename string Name of site on which the intervention operates
  • sitename_printable string Human-readable name of site on which the intervention operates
  • description string Human-readable description of the intervention
  • goals Array<string> List of goals this intervention satisfies

InterventionName

const {InterventionName} = require('flowtypes/intervention_name')

The intervention name, ie facebook/remove_comments

Type: string

Clone this wiki locally