Greasy Fork is available in English.

Variables and Functions

Commonly used variables and functions

Tento skript by nemal byť nainštalovaný priamo. Je to knižnica pre ďalšie skripty, ktorú by mali používať cez meta príkaz // @require https://update.greatest.deepsurf.us/scripts/428386/959223/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, label = 'OpenLinks') => {
  15. $(selector).append(`<button id="open-links" data-count="${count}">${label} (${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.valueOf() === this.valueOf().toUpperCase()
  131. }
  132.  
  133. String.prototype.isLowerCase = function () {
  134. return this.valueOf() === this.valueOf().toLowerCase()
  135. }
  136.  
  137. String.prototype.capitalize = function(preserve = true) {
  138. if(preserve) {
  139. return this.charAt(0).toUpperCase() + this.slice(1)
  140. } else {
  141. let str = this.toLowerCase()
  142. return str.charAt(0).toUpperCase() + str.slice(1)
  143. }
  144. }
  145.  
  146. String.prototype.removeQuotes = function () {
  147. if (this.charAt(0) === '"' && this.charAt(this.length - 1) === '"') {
  148. return this.substr(1, this.length - 2)
  149. }
  150.  
  151. return this
  152. }
  153.  
  154. String.prototype.trimTrailing = function (charlist) {
  155. return this.replace(new RegExp(`[${charlist}]+$`), '')
  156. }
  157.  
  158. String.prototype.nthIndexOf = function (pattern, n) {
  159. var i = -1
  160.  
  161. while (n-- && i++ < this.length) {
  162. i = this.indexOf(pattern, i)
  163. if (i < 0) break
  164. }
  165.  
  166. return i
  167. }
  168.  
  169. String.prototype.insertSpaces = function () {
  170. let str = this
  171.  
  172. str = str.replace(/([a-z])([A-Z])/g, '$1 $2')
  173. str = str.replace(/([A-Z])([A-Z][a-z])/g, '$1 $2')
  174.  
  175. return str
  176. }
  177.  
  178. String.prototype.strToName = function (short, long, ignore) {
  179. var str = this.valueOf()
  180.  
  181. if (ignore.indexOf(str) > -1) {
  182. return ''
  183. } else if (short.indexOf(str) > -1 && short.length == long.length) {
  184. return long[short.indexOf(str)]
  185. } else {
  186. alert('Missing Site')
  187. console.log(`${location.protocol}//${domain}/en/${str}`)
  188.  
  189. return str
  190. }
  191. }
  192.  
  193. String.prototype.contentAfterFirstChar = function (separator) {
  194. return this.substring(this.indexOf(separator) + separator.length)
  195. }
  196.  
  197. $.fn.textOnly = function () {
  198. return $(this).clone().children().remove().end().text()
  199. }
  200.  
  201. $.fn.some = function (fn, thisArg) {
  202. let result
  203.  
  204. for (let i = 0, iLen = this.length; i < iLen; i++) {
  205. if (this.hasOwnProperty(i)) {
  206. if (typeof thisArg == 'undefined') {
  207. result = fn(this[i], i, this)
  208. } else {
  209. result = fn.call(thisArg, this[i], i, this)
  210. }
  211.  
  212. if (result) return true
  213. }
  214. }
  215. return false
  216. }
  217.  
  218. /* Styles */
  219.  
  220. // Compatability style
  221. GM_addStyle('#openLinks {cursor: default; }')
  222.  
  223. GM_addStyle('#open-links {cursor: default; color: black }')
  224. GM_addStyle('#open-links[data-count] {padding: 2px 4px}')
  225. GM_addStyle('#open-links[data-count="1"] {background-color: springgreen}')
  226. GM_addStyle('#open-links[data-count="2"] {background-color: springgreen}')
  227. GM_addStyle('#open-links[data-count="3"] {background-color: springgreen}')
  228. GM_addStyle('#open-links[data-count="4"] {background-color: springgreen}')
  229. GM_addStyle('#open-links[data-count="5"] {background-color: springgreen}')
  230. GM_addStyle('#open-links[data-count="6"] {background-color: springgreen}')
  231. GM_addStyle('#open-links[data-count="7"] {background-color: springgreen}')
  232. GM_addStyle('#open-links[data-count="8"] {background-color: orange}')
  233. GM_addStyle('#open-links[data-count="9"] {background-color: orange}')
  234. GM_addStyle('#open-links[data-count="10"] {background-color: orange}')
  235. GM_addStyle('#open-links[data-count="11"] {background-color: orange}')
  236. GM_addStyle('#open-links[data-count="12"] {background-color: orange}')
  237. GM_addStyle('#open-links[data-count="0"] {background-color: red}')
  238. GM_addStyle('.element--missing {outline: 3px dashed red}')