Skip to content
This repository has been archived by the owner on Jun 15, 2019. It is now read-only.

TBUtils function calls

creesch edited this page Jun 22, 2017 · 4 revisions

TBUtils is one of the core blocks on which toolbox is build. It contains most of functions used to interact with reddit. This page will attempt to explain what each function calls does and how it can be used.

Important: This page is just a quick overview and will not always be entirely up to date, always make sure to check the live code.

Public variables

The public variables defined are mostly used to determine certain states like what page you are on, what version of toolbox this is or if a certain mode is on. The variable names are pretty self explanatory.

Time functions.

TBUtils.minutesToMilliseconds

Feed this thing a javascript epoch and it will return minutes.

TBUtils.timeConverterISO

Feed this thing an UNIX timestamp (reddit provides those in the api) and it will return the time and date in ISO format.

TBUtils.timeConverterRead

Feed this thing a UNIX timestamp and it will return a human readable (assuming you use commie EURO time) time.

TBUtils.humaniseDays

Converts day INT to x days, x weeks and x months.

Misc

TBUtils.title_to_url

This handy little function will make a text suitable for usage in urls. It is used to construct urls like you see them on reddit.

TBUtils.template

Handy little template function. Example:

TBUtils.template('/r/{{subreddit}}/comments/{{link_id}}/{{title}}/', {
    'subreddit': 'toolbox',
    'title':  title_to_url('this is a title we pulled from a post'),
    'link_id': '2kwx2o'
});

Or more likely

var template = '<a href="/r/{{subreddit}}/comments/{{link_id}}/{{title}}/">link</a>';

var url = TBUtils.template(template , {
    'subreddit': 'toolbox',
    'title':  title_to_url('this is a title we pulled from a post'),
    'link_id': '2kwx2o'
});
TBUtils.sortBy

Some sort of sorting

TBUtils.saneSort

Because normal .sort() is case sensitive.

TBUtils.saneSortAs

Ascending sane sort

TBUtils.replaceTokens

Replace single bracket tokens based on context.

TBUtils.forEachChunked

Prevent page lock while parsing things. (stolen from RES)

TBUtils.getThingInfo

Reddit has the habit of presenting the same information in a myriad of ways in the DOM. Posts and comments all used to have a class thing which explains the name of this function. Give this function a thing jquery object and it will scrape as much information out of it as possible regardless of the variation.

Reddit api calls

TBUtils.unescapeJSON

reddit HTML encodes all of their JSON responses, we need to HTMLdecode them before parsing.

TBUtils.getModSubs

Gets the subreddits the current user moderates.

TBUtils.postToWiki
TBUtils.readFromWiki
TBUtils.redditLogin

Depreciated

TBUtils.getBanState
TBUtils.flairPost
TBUtils.flairUser
TBUtils.friendUser
TBUtils.unfriendUser
TBUtils.distinguishThing
TBUtils.approveThing
TBUtils.removeThing
TBUtils.postComment
TBUtils.postLink
TBUtils.sendMessage
TBUtils.sendPM
TBUtils.markMessageRead
TBUtils.aboutUser

Toolbox specific api calls.

TBUtils.exportSettings
TBUtils.importSettings
TBUtils.getReasonsFromCSS

Utility methods

TBUtils.removeQuotes
TBUtils.stringToColor
TBUtils.compressHTML
TBUtils.addToSiteTaable
TBUtils.htmlEncode
TBUtils.htmlDecode
TBUtils.clearCache