Variables and Functions

Commonly used variables and functions

As of 2021-06-24. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/428386/943666/Variables%20and%20Functions.js

  1. //jshint esversion: 6
  2. //jshint asi: true
  3.  
  4. const url = new URL(location.href)
  5. const page = (offset = 0) => url.pathname.split('/')[1 + offset]
  6. const urlObj = url.searchParams
  7. const domain = url.host
  8.  
  9. const setFname = str => {
  10. console.log(str)
  11. return str
  12. }
  13.  
  14. const createBtn = (selector, count = 12) => {
  15. $(selector).append(`<button id="open-links" data-count="${count}">OpenLinks (${count})</button>`)
  16. }
  17.  
  18. const ensureDomLoaded = callback => {
  19. if (['interactive', 'complete'].includes(document.readyState)) {
  20. callback()
  21. return
  22. }
  23.  
  24. let triggered = false
  25. document.addEventListener('DOMContentLoaded', () => {
  26. if (!triggered) {
  27. triggered = true
  28. setTimeout(callback, 1)
  29. }
  30. })
  31. }
  32.  
  33. const awaitElement = function (query, callback, time = null, err = null) {
  34. ensureDomLoaded(() => {
  35. let t = setInterval(() => {
  36. const e = $(query)
  37. if (e.length) {
  38. callback(e)
  39. clearInterval(t)
  40. return
  41. }
  42.  
  43. if (time !== null) {
  44. setTimeout(() => {
  45. clearInterval(t)
  46. err(e)
  47. return
  48. }, time)
  49. }
  50. }, 10)
  51. })
  52. }
  53.  
  54. const awaitTitleChange = (value, callback) => {
  55. let t = setInterval(() => {
  56. e = document.title
  57. if (e !== value) {
  58. callback(e)
  59. clearInterval(t)
  60. }
  61. }, 10)
  62. }
  63.  
  64. const keyboardEvent = (callback, key = 'F19') => {
  65. document.addEventListener('keydown', e => {
  66. if (e.key.toLowerCase() === key.toLowerCase()) callback()
  67. })
  68. }
  69.  
  70. const onFocus = (callback, persistent = false) => {
  71. if (persistent) {
  72. $(window).on('focus', callback)
  73. return
  74. } else if (document.hasFocus()) {
  75. callback()
  76. return
  77. }
  78.  
  79. $(window).one('focus', callback)
  80. }
  81.  
  82. const onBlur = (callback, persistent = false) => {
  83. if (persistent) {
  84. $(window).on('blur', callback)
  85. return
  86. } else if (!document.hasFocus()) {
  87. callback()
  88. return
  89. }
  90.  
  91. $(window).one('blur', callback)
  92. }
  93.  
  94. const onBlur_closeWindow = (persistent = false) => onBlur(window.close, persistent)
  95. const onFocus_setClipboard = (data, persistent = false) => onFocus(() => setClipboard(data), persistent)
  96. const setClipboard = data => GM_setClipboard(data)
  97. const defaultCase = () => console.log(domain)
  98.  
  99. /** Prototypes **/
  100. Array.prototype.hasItem = function (item) {
  101. return this.indexOf(item) !== -1
  102. }
  103.  
  104. Array.prototype.findDuplicates = function () {
  105. var uniq = this.map(name => {
  106. return {
  107. count: 1,
  108. name: name
  109. }
  110. }).reduce((a, b) => {
  111. a[b.name] = (a[b.name] || 0) + b.count
  112. return a
  113. }, {})
  114.  
  115. return Object.keys(uniq).filter(a => uniq[a] > 1)
  116. }
  117.  
  118. String.prototype.sizeToBytes = function () {
  119. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
  120.  
  121. var valueArr = this.valueOf().split(' ')
  122.  
  123. var selectedSize = valueArr[0]
  124. var selectedType = valueArr[1]
  125.  
  126. return selectedSize * Math.pow(1024, sizes.indexOf(selectedType))
  127. }
  128.  
  129. String.prototype.isUpperCase = function () {
  130. return this === this.toUpperCase()
  131. }
  132.  
  133. String.prototype.isLowerCase = function () {
  134. return this === this.toLowerCase()
  135. }
  136.  
  137. String.prototype.removeQuotes = function () {
  138. if (this.charAt(0) === '"' && this.charAt(this.length - 1) === '"') {
  139. return this.substr(1, this.length - 2)
  140. }
  141.  
  142. return this
  143. }
  144.  
  145. String.prototype.trimTrailing = function (charlist) {
  146. return this.replace(new RegExp(`[${charlist}]+$`), '')
  147. }
  148.  
  149. String.prototype.nthIndexOf = function (pattern, n) {
  150. var i = -1
  151.  
  152. while (n-- && i++ < this.length) {
  153. i = this.indexOf(pattern, i)
  154. if (i < 0) break
  155. }
  156.  
  157. return i
  158. }
  159.  
  160. String.prototype.insertSpaces = function () {
  161. let str = this
  162.  
  163. str = str.replace(/([a-z])([A-Z])/g, '$1 $2')
  164. str = str.replace(/([A-Z])([A-Z][a-z])/g, '$1 $2')
  165.  
  166. return str
  167. }
  168.  
  169. String.prototype.strToName = function (short, long, ignore) {
  170. var str = this.valueOf()
  171.  
  172. if (ignore.indexOf(str) > -1) {
  173. return ''
  174. } else if (short.indexOf(str) > -1 && short.length == long.length) {
  175. return long[short.indexOf(str)]
  176. } else {
  177. alert('Missing Site')
  178. console.log(`${location.protocol}//${domain}/en/${str}`)
  179.  
  180. return str
  181. }
  182. }
  183.  
  184. String.prototype.contentAfterFirstChar = function (separator) {
  185. return this.substring(this.indexOf(separator) + separator.length)
  186. }
  187.  
  188. $.fn.textOnly = function () {
  189. return $(this).clone().children().remove().end().text()
  190. }
  191.  
  192. $.fn.some = function (fn, thisArg) {
  193. let result
  194.  
  195. for (let i = 0, iLen = this.length; i < iLen; i++) {
  196. if (this.hasOwnProperty(i)) {
  197. if (typeof thisArg == 'undefined') {
  198. result = fn(this[i], i, this)
  199. } else {
  200. result = fn.call(thisArg, this[i], i, this)
  201. }
  202.  
  203. if (result) return true
  204. }
  205. }
  206. return false
  207. }