InstaSynchP Library

Basic function that are needed by several scripts use with @require

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/5647/51269/InstaSynchP%20Library.js

  1. // ==UserScript==
  2. // @name InstaSynchP Library
  3. // @namespace InstaSynchP
  4. // @description Basic function that are needed by several scripts use with @require
  5.  
  6. // @version 1.1.8
  7. // @author Zod-
  8. // @source https://github.com/Zod-/InstaSynchP-Library
  9. // @license MIT
  10.  
  11. // @include *://instasync.com/r/*
  12. // @include *://*.instasync.com/r/*
  13. // @grant none
  14. // @run-at document-start
  15. // ==/UserScript==
  16.  
  17. //http://joquery.com/2012/string-format-for-javascript
  18. if (typeof String.prototype.format !== 'function') {
  19. String.prototype.format = function () {
  20. // The string containing the format items (e.g. "{0}")
  21. // will and always has to be the first argument.
  22. var theString = this,
  23. i,
  24. regEx;
  25.  
  26. // start with the second argument (i = 1)
  27. for (i = 0; i < arguments.length; i += 1) {
  28. // "gm" = RegEx options for Global search (more than one instance)
  29. // and for Multiline search
  30. regEx = new RegExp("\\{" + (i) + "\\}", "gm");
  31. theString = theString.replace(regEx, arguments[i]);
  32. }
  33. return theString;
  34. };
  35. }
  36. //http://stackoverflow.com/a/646643
  37. if (typeof String.prototype.startsWith !== 'function') {
  38. String.prototype.startsWith = function (str) {
  39. return this.slice(0, str.length) == str;
  40. };
  41. }
  42. if (typeof String.prototype.endsWith != 'function') {
  43. String.prototype.endsWith = function (str) {
  44. return this.slice(-str.length) == str;
  45. };
  46. }
  47.  
  48. //http://stackoverflow.com/a/1978419
  49. if (typeof String.prototype.contains !== 'function') {
  50. String.prototype.contains = function (it) {
  51. return this.indexOf(it) !== -1;
  52. };
  53. }
  54. if (typeof Array.prototype.contains !== 'function') {
  55. Array.prototype.contains = function (it) {
  56. return this.indexOf(it) !== -1;
  57. };
  58. }
  59.  
  60. function isUdef(obj) {
  61. "use strict";
  62. return typeof obj === 'undefined';
  63. }
  64.  
  65. function commonPrefix(array) {
  66. "use strict";
  67. if (typeof array === 'undefined' || array.length === 0) {
  68. return;
  69. }
  70. var sorted = array.slice(0).sort(function (a, b) {
  71. return a.toLowerCase().localeCompare(b.toLowerCase());
  72. }),
  73. first = sorted[0],
  74. firstLower = first.toLowerCase(),
  75. last = sorted[sorted.length - 1].toLowerCase(),
  76. L = first.length,
  77. i = 0;
  78. while (i < L && firstLower.charAt(i) === last.charAt(i)) i++;
  79. return first.substring(0, i);
  80. }
  81.  
  82. function isBlackname(username) {
  83. "use strict";
  84. if (typeof username !== 'string') {
  85. return false;
  86. }
  87. return username.match(/^(?:[A-Za-z0-9]|(?:[\-_](?![\-_]))){5,16}$/) !== null;
  88. }
  89.  
  90. function isGreyname(username) {
  91. "use strict";
  92. if (typeof username !== 'string') {
  93. return false;
  94. }
  95. return username.match(/^(?:[A-Za-z0-9]|(?:[\-_](?![\-_]))){1,16}$/) !== null;
  96. }
  97.  
  98. function htmlDecode(value) {
  99. "use strict";
  100. return $('<div/>').html(value).text();
  101. }
  102.  
  103. function activeVideoIndex() {
  104. "use strict";
  105. return $('#playlist .active').index();
  106. }
  107.  
  108. function findUserId(id) {
  109. "use strict";
  110. var i, users = window.room.userlist.users;
  111. for (i = 0; i < users.length; i += 1) {
  112. if (id === users[i].id) {
  113. return users[i];
  114. }
  115. }
  116. return undefined;
  117. }
  118.  
  119. function findUserUsername(name) {
  120. "use strict";
  121. name = name.toLowerCase();
  122. var i, users = window.room.userlist.users;
  123. for (i = 0; i < users.length; i += 1) {
  124. if (name === users[i].username.toLowerCase()) {
  125. return users[i];
  126. }
  127. }
  128. return undefined;
  129. }
  130.  
  131. function videojs() {
  132. "use strict";
  133. return $('.video-js')[0];
  134. }
  135.  
  136. function sendcmd(cmd, opts) {
  137. "use strict";
  138. window.room.sendcmd(cmd, opts);
  139. }
  140.  
  141. function reloadPlayer() {
  142. "use strict";
  143. if (window.room.video) {
  144. try {
  145. window.room.video.destroy();
  146. } catch (ignore) {}
  147. }
  148. sendcmd('reload', null);
  149. }
  150.  
  151. function addSystemMessage(message) {
  152. "use strict";
  153. window.room.addMessage({
  154. username: ""
  155. }, message, 'text-info');
  156. }
  157.  
  158. function addErrorMessage(message) {
  159. "use strict";
  160. window.room.addMessage({
  161. username: ""
  162. }, message, 'text-danger');
  163. }
  164.  
  165. function videoInfoEquals(a, b) {
  166. "use strict";
  167. if (!a || !b) {
  168. return false;
  169. }
  170. return (a.provider && a.provider === b.provider &&
  171. a.mediaType && a.mediaType === b.mediaType &&
  172. a.id && a.id === b.id);
  173. }
  174.  
  175. function scrollDown() {
  176. "use strict";
  177. $('#chat_messages').scrollTop($('#chat_messages')[0].scrollHeight);
  178. }
  179.  
  180. function isMod(username) {
  181. "use strict";
  182. if (isUdef(username)) {
  183. return window.room.user.isMod;
  184. } else {
  185. return findUserUsername(username).permissions > 0;
  186. }
  187. }
  188.  
  189. function thisUser() {
  190. "use strict";
  191. return room.user.userinfo;
  192. }
  193.  
  194. function logger() {
  195. "use strict";
  196. return window.plugins.logger.log;
  197. }
  198.  
  199. function createNavTab(op) {
  200. "use strict";
  201. return $('<li>').append(
  202. $('<a>', {
  203. class: 'active_tooltip',
  204. 'data-original-title': op.tooltip,
  205. href: op.tab,
  206. 'data-toggle': 'tab',
  207. rel: 'tooltip',
  208. 'data-placement': op.tooltipPlacement || 'bottom'
  209. }).append(
  210. $('<i>', {
  211. class: op.class
  212. })
  213. ).tooltip()
  214. );
  215. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址