Variables and Functions

Commonly used variables and functions

Från och med 2021-07-22. Se den senaste versionen.

Detta skript bör inte installeras direkt. Det är ett bibliotek för andra skript att inkludera med meta-direktivet // @require https://update.greatest.deepsurf.us/scripts/428386/953149/Variables%20and%20Functions.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

  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. }
  208.  
  209. /* Styles */
  210.  
  211. // Compatability style
  212. GM_addStyle('#openLinks {cursor: default; }')
  213.  
  214. GM_addStyle('#open-links {cursor: default; color: black }')
  215. GM_addStyle('#open-links[data-count] {padding: 2px 4px}')
  216. GM_addStyle('#open-links[data-count="1"] {background-color: springgreen}')
  217. GM_addStyle('#open-links[data-count="2"] {background-color: springgreen}')
  218. GM_addStyle('#open-links[data-count="3"] {background-color: springgreen}')
  219. GM_addStyle('#open-links[data-count="4"] {background-color: springgreen}')
  220. GM_addStyle('#open-links[data-count="5"] {background-color: springgreen}')
  221. GM_addStyle('#open-links[data-count="6"] {background-color: springgreen}')
  222. GM_addStyle('#open-links[data-count="7"] {background-color: springgreen}')
  223. GM_addStyle('#open-links[data-count="8"] {background-color: orange}')
  224. GM_addStyle('#open-links[data-count="9"] {background-color: orange}')
  225. GM_addStyle('#open-links[data-count="10"] {background-color: orange}')
  226. GM_addStyle('#open-links[data-count="11"] {background-color: orange}')
  227. GM_addStyle('#open-links[data-count="12"] {background-color: orange}')
  228. GM_addStyle('#open-links[data-count="0"] {background-color: red}')
  229. GM_addStyle('.element--missing {outline: 3px dashed red}')