Greasy Fork is available in English.

Utils - Library

some utilities for easier developing

Tätä skriptiä ei tulisi asentaa suoraan. Se on kirjasto muita skriptejä varten sisällytettäväksi metadirektiivillä // @require https://greasyfork.org/scripts/451088-utils-library/code/Utils%20-%20Library.js?version=1097324.

Tekijä
NotYou
Versio
1.1.0
Luotu
10.9.2022
Päivitetty
15.9.2022
Lisenssi
LGPL-3.0

Utils

Description:

some utilities for easier developing

How to use:

  1. Copy Library's link: // @require https://greasyfork.org/scripts/451088/code/utils.js
  2. Add // @require https://greasyfork.org/scripts/451088/code/utils.js to your script meta info.
  3. Enjoy Utilities (Documentation below).

Docs

If you see after type of argument *, then that argument is optional, so you can not use it.


capitalize

Argument 1: string

Returns: string

Converts the first letter in a string to uppercase.

utils.capitalize('hello!') // Output: 'Hello!'

startsWith

Argument 1: string
Argument 2: number | string | object *

Returns: boolean

Checks if argument 1 starts with argument 2.

utils.startsWith('hello', 'he') // Output: true
utils.startsWith('hello', 'llo') // Output: false

endsWith

Argument 1: string
Argument 2: number | string | object *

Returns: boolean

Checks if argument 1 ends with argument 2.

utils.endsWith('hello', 'he') // Output: false
utils.endsWith('hello', 'llo') // Output: true

at

Argument 1: number | string | object
Argument 2: number

Returns: any

Returns value that stored in argument 1 at argument 2 index.

utils.at([3, 2, 1], 0) // Output: 3
utils.at([3, 2, 1], -1) // Output: 1

querySelector

Argument 1: string
Argument 2: Element *

Returns: Element | null

Returns element from document with selector that equals to argument 1. Argument 2 is parent node

utils.querySelector('html') // Output: <html lang="en">...
utils.querySelector('body', utils.querySelector('html')) // Output: <body>...

querySelectorAll

Argument 1: string
Argument 2: Element *

Returns: NodeList | null

Returns node list from document with selector that equals to argument 1. Argument 2 is parent node

utils.querySelectorAll('div') // Output: [<div>..., <div>..., ...]

buildCSSSelector

Argument 1: HTMLElement

Returns: string

Returns CSS selector that leads to element.

utils.buildCSSSelector(utils.querySelector('head > link')) // Output: "html > head > link"

try

Argument 1: Function

Returns: undefined

Runs function in try { ... } catch() { ... } block to make sure that code doesn't will give error. Use in places where expected error.

utils.try(function() {
  console.logg('Hello!') // Here is Error, but code will continue to work
})

console.log('hello again!')

open

Argument 1: string
Argument 2: boolean *

Returns: undefined

Opens link that should be written in argument 1. Argument 2 used to choose open page in new tab or not (default: false).

utils.open('https://greasyfork.org/en/users/824432') // New Tab

utils.open('https://greasyfork.org/en/users/824432', false) // This Tab

getGlobal

Returns: object

Returns global object.

utils.getGlobal() // Window ...

append

Argument 1: HTMLElement
Argument 2: HTMLElement

Returns: undefined

Append argument 1 to argument 2.

utils.append(document.querySelector('img'), document.querySelector('a'))

isjQueryExists

Returns: boolean

Returns true if jQuery exists on page, otherwise false.

utils.isjQueryExists() // Output: true or false

UUID

Returns: string

Returns UUIDv4 (xxxxxxxx-xxxx-4xxx-(8|9|a|b)xxx-xxxxxxxxxxxx).

utils.UUID() // Output: 155ae6c7-a97c-444b-b588-ee0d2fcea19f

onLoad

Argument 1: Function

Returns: undefined

Initializes function when page is loaded and interactive.

utils.onLoad(function() {
  console.log('hello world!')
})

spaceTrim

Argument 1: string

Returns: string

Returns string without needless spaces.

utils.spaceTrim('hello          world!') // Output: "hello world!"

waitForElement

Argument 1: string

Returns: HTMLElement

Waits util element will exist on page, and then returns it.

utils.waitForElement('body > div').then(function() {
  document.querySelector('body').style.background = 'red'
})

delay

Argument 1: Function
Argument 2: number *

Returns: undefined

Initializes function after some time. You can set that time in argument 2. (Milliseconds)

var now = Date.now()

utils.delay(function() {
  console.log('Hi! Im runned after', Math.round((Date.now() - now) / 1000), 'seconds')
}, 2000)

1.1.0

reload

Returns: undefined

Reloads page.

utils.reload()

getFunctionName

Argument 1: Function

Returns: string | null

Returns string if function has name, otherwise, returns null.

utils.getFunctionName(function helloWorld() {}) // Output: 'helloWorld'
utils.getFunctionName(function () {}) // Output: null

time

Argument 1: object

Returns: number

Returns total milliseconds from object values.

utils.time({
  year: 1,
  month: 5,
  week: 42,
  day: 2,
  hour: 72,
  minute: 3,
  second: 30,
  millisecond: 1337,
  someOtherKey: 'someOtherValue', // that line will be ignored.
})

// Output: 188880737337

utils.time({
  minute: 1,
  seconds: 30,
})

// Output: 60000

createElement

Argument 1: string
Argument 2: object *

Returns: Element

Returns element with tag name that equals to argument 1, argument 2 is used to set attributes.

utils.createElement('h1', {
  title: 'Some Title',
  'data-is-h1-tag': true
})

// Output: <h1 title="Some Title" data-is-h1-tag="true">...