Skip to content

Instantly share code, notes, and snippets.

@creesch
Last active May 31, 2019 08:57
Show Gist options
  • Select an option

  • Save creesch/f1072c9ffd010ffd4c44986953c1f390 to your computer and use it in GitHub Desktop.

Select an option

Save creesch/f1072c9ffd010ffd4c44986953c1f390 to your computer and use it in GitHub Desktop.

Modules

CommentNuke
QueueTools

Classes

TBListener

Objects

TBui : object
TBUtils : object

CommentNuke

CommentNuke~parseComments(object, postID, subreddit, callback)

Will given a reddit API comment object go through the chain and put all comments

Kind: inner method of CommentNuke

Param Type Description
object object

Comment chain object

postID string

Post id the comments belong to

subreddit string

Subreddit the comment chain belongs to.

callback function

QueueTools

QueueTools~getModlog(subreddit, callback)

Fetches the modlog for a subreddit and updates modlogCache.

Kind: inner method of QueueTools

Param Type Description
subreddit string

the subreddit for which the modlog needs to be fetched

callback getModlogCallback

callback that handles further modlog interactions

QueueTools~checkForActions(subreddit, fullName) ⇒ false | object

Checks modLogCache for actions on the given fullName and subreddit.

Kind: inner method of QueueTools
Returns: false | object -

Either false or an object with actions

Param Type Description
subreddit string

The subreddit the fullName thing belongs to.

fullName string

Thing (post/comment) fullName

QueueTools~getActions(subreddit, fullName, callback)

Checks for mod actions on the given fullName thing and subreddit through a caching mechanism.

Kind: inner method of QueueTools

Param Type Description
subreddit string

the subreddit for which the modlog needs to be fetched

fullName string

thing (post/comment) fullName

callback getActionsCallback

callback that handles further modlog interactions

QueueTools~getModlogCallback : function

Callback for further handling the modlog.

Kind: inner typedef of QueueTools

QueueTools~getActionsCallback : function

Callback for further handling the modlog.

Kind: inner typedef of QueueTools

Param Type Description
result Boolean | Object

Either false or an object with actions

TBListener

Kind: global class

new TBListener()

Create a new instance of TBListener. Nothing happens yet until TBListener.start() has been called

tbListener.start()

Starts the TBListener instance by registering an event listener for reddit events

A TBListenerLoaded event is fired when everything is ready.

Kind: instance method of TBListener

tbListener.stop()

Unregisters this instance's event listener

Kind: instance method of TBListener

tbListener.on(Name, Callback)

Register an event listener for a given event name for a callback.

Kind: instance method of TBListener

Param Type Description
Name string

of event

Callback listenerCallback

tbListener.clear(task) ⇒ Boolean

Clears a scheduled 'read' or 'write' task.

Kind: instance method of TBListener
Returns: Boolean -

success


Access: public

Param Type
task Object

TBListener~listenerCallback : function

Callback for a reddit event. The callback's this is event.target

Kind: inner typedef of TBListener
this: HTMLElement

Param Type
event CustomEvent
responseMessage string

TBui : object

Kind: global namespace

TBui.contextTrigger(triggerId, options)

Add or remove a menu element to the context aware menu. Makes the menu shows if it was empty before adding, hides menu if it is empty after removing.

Kind: static method of TBui

Param Type Description
triggerId string

This will be part of the id given to the element.

options object
options.addTrigger boolean

Indicates of the menu item needs to be added or removed.

options.triggerText string

Text displayed in menu. Not needed when addTrigger is false.

options.triggerIcon string

The material icon that needs to be displayed before the menu item. Defaults to 'label'

options.title string

Title to be used in title attribute. If no title is given the triggerText will be used.

options.dataAttributes object

Any data attribute that might be needed. Object keys will be used as the attribute name and value as value.

TBui.tbRedditEvent($elements, types)

Will send out events similar to the reddit jsAPI events for the elements given. Only support 'comment' for now and will only send the commentAuthor event.

Kind: static method of TBui

Param Type Description
$elements object

jquery object containing the e

types string

comma seperated list of type of elements the events need to be send for.

TBui.makeSubmissionEntry(submission, submissionOptions) ⇒ object

Will build a submission entry given a reddit API submission object.

Kind: static method of TBui
Returns: object -

jquery object with the build submission.

Param Type Description
submission object

reddit API submission object.

submissionOptions object

object denoting what needs to be included.

TBui.makeSingleComment(comment, commentOptions) ⇒ object

Will build a comment given a reddit API comment object.

Kind: static method of TBui
Returns: object -

jquery object with the build comment.

Param Type Description
comment object

reddit API comment object.

commentOptions object

object denoting what needs to be included. Object can contain 'parentLink', 'contextLink' and 'fullCommentsLink' as boolean.

TBui.makeCommentThread(jsonInput, commentOptions) ⇒ object

Will build a comment given a reddit API comment object.

Kind: static method of TBui
Returns: object -

jquery object with the build comment thread.

Param Type Description
jsonInput object

reddit API comments object.

commentOptions object

object denoting what needs to included. Object can contain 'parentLink', 'contextLink' and 'fullCommentsLink' as boolean.

TBUtils : object

Kind: global namespace

TBUtils.baseDomain : string

If we are on new modmail we use www.reddit.com for all other instances we use whatever is the current domain. Used because some browsers do not like relative urls in extensions

Kind: static property of TBUtils

TBUtils.tempBaseDomain : string

Pretty much as the name suggests

Kind: static property of TBUtils
Todo

  • Once the redesign is no longer alpha/beta switch all instances that use this.

TBUtils.apiOauthPOST

Sends an authenticated POST request against the OAuth API.

Kind: static property of TBUtils

Param Type Description
endpoint string

The endpoint to request

data object

Query parameters as an object

TBUtils.apiOauthGET

Sends an authenticated GET request against the OAuth API.

Kind: static property of TBUtils

Param Type Description
endpoint string

The endpoint to request

data object

Query parameters as an object

TBUtils.link(link) ⇒ string

Takes an absolute path for a link and prepends the www.reddit.com domain if we're in new modmail (mod.reddit.com). Makes absolute path links work everywhere.

Kind: static method of TBUtils

Param Type Description
link string

The link path, starting with "/"

TBUtils.debugInformation() ⇒ debugObject

Puts important debug information in a object so we can easily include it in /r/toolbox posts and comments when people need support.

Kind: static method of TBUtils
Returns: debugObject -

Object with debug information


TBUtils.getToolboxDevs() ⇒ array

Fetches the toolbox dev from /r/toolbox or falls back to a predefined list.

Kind: static method of TBUtils
Returns: array -

List of toolbox devs


TBUtils.moveArrayItem(array, old_index, new_index) ⇒ array

Moves an item in an array from one index to another https://github.com/brownieboy/array.prototype.move/blob/master/src/array-prototype-move.js

Kind: static method of TBUtils
Returns: array -

New array with moved items

Param Type Description
array array

input array

old_index integer
new_index integer

TBUtils.escapeHTML(html) ⇒ string

Escape html entities

Kind: static method of TBUtils
Returns: string -

HTML string with escaped entities

Param Type Description
html string

input html

TBUtils.unescapeHTML(html) ⇒ string

Unescape html entities

Kind: static method of TBUtils
Returns: string -

HTML string with unescaped entities

Param Type Description
html string

input html

TBUtils.getTime() ⇒ integer

Give the nummeric value in milliseconds of the current date and time.

Kind: static method of TBUtils
Returns: integer -

time value in milliseconds


TBUtils.getRandomNumber(maxInt) ⇒ integer

Give a random number

Kind: static method of TBUtils
Returns: integer -

random number

Param Type Description
maxInt integer

Max integer

TBUtils.minutesToMilliseconds(mins) ⇒ integer

Convert minutes to milliseconds

Kind: static method of TBUtils
Returns: integer -

Milliseconds

Param Type Description
mins integer

Minutes

TBUtils.daysToMilliseconds(days) ⇒ integer

Convert days to milliseconds

Kind: static method of TBUtils
Returns: integer -

Milliseconds

Param Type Description
days integer

days

TBUtils.millisecondsToDays(milliseconds) ⇒ integer

Convert milliseconds to days

Kind: static method of TBUtils
Returns: integer -

Days

Param Type Description
milliseconds integer

milliseconds

TBUtils.timeConverterISO(UNIX_timestamp) ⇒ string

convert unix epoch timestamps to ISO format

Kind: static method of TBUtils
Returns: string -

ISO formatted time

Param Type Description
UNIX_timestamp integer

Unix timestamp

TBUtils.niceDateDiff(origdate, newdate) ⇒ string

Returns the difference between days in nice format like "1 year"

Kind: static method of TBUtils
Returns: string -

Formatted date difference

Param Type
origdate Date
newdate Date

TBUtils.timeConverterRead(UNIX_timestamp) ⇒ string

convert unix epoch timestamps to readable format dd-mm-yyyy hh:mm:ss UTC

Kind: static method of TBUtils
Returns: string -

Formatted date in dd-mm-yyyy hh:mm:ss UTC

Param Type
UNIX_timestamp integer

TBUtils.title_to_url(title) ⇒ string

convert titles to a format usable in urls from r2.lib.utils import title_to_url

Kind: static method of TBUtils
Returns: string -

Formatted title

Param Type
title string

TBUtils.alert(message, callback, showClose) ⇒ callback

Opens the toolbox "nag" alert everyone loves so much. USE SPARINGLY

Kind: static method of TBUtils
Returns: callback -

callback with true or false in parameter which will be called when the alert is closed.

Param Type Description
message string
callback callback

callback function

showClose boolean

If true the alert can be dismissed by a clost button otherwise it needs to be clicked.

TBUtils.notification(title, body, path, markreadid)

Shows a notification, uses native browser notifications if the user allows it or falls back on html notifications.

Kind: static method of TBUtils

Param Type Description
title string

Notification title

body string

Body text

path string

Absolute path to be opend when clicking the notification

markreadid string

The ID of a conversation to mark as read when the notification is clicked

TBUtils.humaniseDays(days) ⇒ string

Converts a given amount of days in a "humanized version" of weeks, months and years.

Kind: static method of TBUtils
Returns: string -

x year x months x weeks x day

Param Type
days integer

TBUtils.stringFormat()

Kind: static method of TBUtils
Todo

  • properly describe what this does

TBUtils.sortBy(arr, prop)

Sorts an array of objects by property value of specific properties.

Kind: static method of TBUtils

Param Type Description
arr array

input array

prop string

property name

TBUtils.saneSort(arr)

Because normal .sort() is case sensitive.

Kind: static method of TBUtils

Param Type Description
arr array

input array

TBUtils.saneSortAs(arr)

Because normal .sort() is case sensitive and we also want to sort ascending from time to time.

Kind: static method of TBUtils

Param Type Description
arr array

input array

TBUtils.replaceAll(find, replace, str) ⇒ string

Replace all instances of a certaing thing for another thing.

Kind: static method of TBUtils
Returns: string -

shiny new string with replaced stuff

Param Type Description
find string

what to find

replace string

what to replace

str string

where to do it all with

TBUtils.colorNameToHex(color) ⇒ string

Will compare the input color to a list of known color names and return the HEX value

Kind: static method of TBUtils
Returns: string -

if a match is found the HEX color, otherwise the input string.

Param Type Description
color string

input color

TBUtils.removeLastDirectoryPartOf(url) ⇒ string

strips the last directory part of an url. Example: /this/is/url/with/part/ becomes /this/is/url/with/

Kind: static method of TBUtils
Returns: string -

url without the last directory part

Param Type Description
url string

reddit API comment object.

TBUtils.isOdd(num) ⇒ boolean

Will tell if a number is odd

Kind: static method of TBUtils
Returns: boolean -

true if number is odd false if even.

Param Type Description
num integer

reddit API comment object.

TBUtils.cleanSubredditName(dirtySub) ⇒ string

Because there are a ton of ways how subreddits are written down and sometimes we just want the name.

Kind: static method of TBUtils
Returns: string -

shiny sub!

Param Type Description
dirtySub string

dirty dirty sub.

TBUtils.sendRequest(options)

Sends a generic HTTP request through the background page.

Kind: static method of TBUtils

Param Type Description
options object

The options for the AJAX request

options.method string

The HTTP method to use for the request

options.endpoint string

The endpoint to request

options.data object

Query parameters as an object

options.oauth boolean

If true, the request will be sent on oauth.reddit.com, and the Authorization header will be set with the OAuth access token for the logged-in user

TBUtils.getJSON(endpoint, data)

Performs a GET request and promises the body of the response, or the full response object on error. Maintains an API similar to $.getJSON() because that's what all these calls used before Chrome forced us to make all requests in the background.

Kind: static method of TBUtils

Param Type Description
endpoint string

The endpoint to request

data object

Query parameters as an object

TBUtils.post(endpoint, data)

Performs a POST request and promises the body of the response, or the full response object on error. Maintains an API similar to $.post.

Kind: static method of TBUtils

Param Type Description
endpoint string

The endpoint to request

data object

The body of the request.

TBUtils.getHead(endpoint, doneCallback) ⇒ callback

Perform a HEAD request.

Kind: static method of TBUtils
Todo

  • Implement with promises (consumers need to be updated)
  • "get head" is a confusing name
Param Type Description
endpoint string

The endpoint to request

doneCallback callback

TBUtils.apiOauthRequest(method, endpoint, data)

Sends an authenticated request against the OAuth API from the background page.

Kind: static method of TBUtils

Param Type Description
method string

An HTTP verb

endpoint string

The endpoint to request

data object

Query parameters as an object

TBUtils.debugObject : Object

Kind: static typedef of TBUtils
Properties

Name Type Description
toolboxVersion string

The toolbox version

browser string

Browser used (Firefox, Chrome, etc)

browserVersion string

The version of the browser

platformInformation string

Other platform information

betaMode boolean

toolbox beta mode enabled

debugMode boolean

toolbox debugMode enabled

compactMode boolean

toolbox compactmode enabled

advancedSettings boolean

toolbox advanced settings enabled

cookiesEnabled boolean

Browser cookies enabled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment