util.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import 'assets://js/lib/uri.min.js'
  2. import cheerio from 'assets://js/lib/cheerio.min.js';
  3. import 'assets://js/lib/crypto-js.js'
  4. import 'assets://js/lib/dayjs.min.js'
  5. import _ from 'assets://js/lib/underscore-esm-min.js'
  6. var charStr = 'abacdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789';
  7. export function randIndex(min, max, i) {
  8. let index = Math.floor(Math.random() * (max - min + 1) + min),
  9. numStart = charStr.length - 10;
  10. if (i == 0 && index >= numStart) {
  11. index = randIndex(min, max, i);
  12. }
  13. return index;
  14. }
  15. export function randomStr(len) {
  16. let min = 0, max = charStr.length - 1, _str = '';
  17. len = len || 15;
  18. for (var i = 0, index; i < len; i++) {
  19. index = randIndex(min, max, i);
  20. _str += charStr[index];
  21. }
  22. return _str;
  23. }
  24. if (typeof Object.assign != 'function') {
  25. Object.assign = function () {
  26. var target = arguments[0];
  27. for (var i = 1; i < arguments.length; i++) {
  28. var source = arguments[i];
  29. for (var key in source) {
  30. if (Object.prototype.hasOwnProperty.call(source, key)) {
  31. target[key] = source[key];
  32. }
  33. }
  34. }
  35. return target;
  36. };
  37. }
  38. if (!String.prototype.includes) {
  39. String.prototype.includes = function (search, start) {
  40. if (typeof start !== 'number') {
  41. start = 0;
  42. }
  43. if (start + search.length > this.length) {
  44. return false;
  45. } else {
  46. return this.indexOf(search, start) !== -1;
  47. }
  48. };
  49. }
  50. if (!Array.prototype.includes) {
  51. Object.defineProperty(Array.prototype, 'includes', {
  52. value: function (searchElement, fromIndex) {
  53. if (this == null) {//this是空或者未定义,抛出错误
  54. throw new TypeError('"this" is null or not defined');
  55. }
  56. var o = Object(this);//将this转变成对象
  57. var len = o.length >>> 0;//无符号右移0位,获取对象length属性,如果未定义就会变成0
  58. if (len === 0) {//length为0直接返回false未找到目标值
  59. return false;
  60. }
  61. var n = fromIndex | 0;//查找起始索引
  62. var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);//计算正确起始索引,因为有可能是负值
  63. while (k < len) {//从起始索引处开始循环
  64. if (o[k] === searchElement) {//如果某一位置与寻找目标相等,返回true,找到了
  65. return true;
  66. }
  67. k++;
  68. }
  69. return false;//未找到,返回false
  70. }
  71. });
  72. }
  73. if (typeof String.prototype.startsWith != 'function') {
  74. String.prototype.startsWith = function (prefix) {
  75. return this.slice(0, prefix.length) === prefix;
  76. };
  77. }
  78. if (typeof String.prototype.endsWith != 'function') {
  79. String.prototype.endsWith = function (suffix) {
  80. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  81. };
  82. }
  83. Object.prototype.myValues = function (obj) {
  84. if (obj == null) {
  85. throw new TypeError("Cannot convert undefined or null to object");
  86. }
  87. var res = []
  88. for (var k in obj) {
  89. if (obj.hasOwnProperty(k)) {//需判断是否是本身的属性
  90. res.push(obj[k]);
  91. }
  92. }
  93. return res;
  94. }
  95. if (typeof Object.prototype.values != 'function') {
  96. Object.prototype.values = function (obj) {
  97. if (obj == null) {
  98. throw new TypeError("Cannot convert undefined or null to object");
  99. }
  100. var res = []
  101. for (var k in obj) {
  102. if (obj.hasOwnProperty(k)) {//需判断是否是本身的属性
  103. res.push(obj[k]);
  104. }
  105. }
  106. return res;
  107. }
  108. }
  109. if (typeof Array.prototype.join != 'function') {
  110. Array.prototype.join = function (emoji) {
  111. // emoji = emoji||',';
  112. emoji = emoji || '';
  113. let self = this;
  114. let str = "";
  115. let i = 0;
  116. if (!Array.isArray(self)) { throw String(self) + 'is not Array' }
  117. if (self.length === 0) { return '' }
  118. if (self.length === 1) { return String(self[0]) }
  119. i = 1;
  120. str = this[0];
  121. for (; i < self.length; i++) {
  122. str += String(emoji) + String(self[i]);
  123. }
  124. return str;
  125. };
  126. }
  127. String.prototype.rstrip = function (chars) {
  128. let regex = new RegExp(chars + "$");
  129. return this.replace(regex, "");
  130. };
  131. Array.prototype.append = Array.prototype.push;
  132. String.prototype.strip = String.prototype.trim;
  133. export function 是否正版(vipUrl) {
  134. let flag = new RegExp('qq\.com|iqiyi\.com|youku\.com|mgtv\.com|bilibili\.com|sohu\.com|ixigua\.com|pptv\.com|miguvideo\.com|le\.com|1905\.com|fun\.tv');
  135. return flag.test(vipUrl);
  136. }
  137. export function urlDeal(vipUrl) {
  138. if (!vipUrl) {
  139. return ''
  140. }
  141. if (!是否正版(vipUrl)) {
  142. return vipUrl
  143. }
  144. if (!/miguvideo/.test(vipUrl)) {
  145. vipUrl = vipUrl.split('#')[0].split('?')[0];
  146. }
  147. return vipUrl
  148. }
  149. export function urlencode(str) {
  150. str = (str + '').toString();
  151. return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
  152. replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
  153. }
  154. export function base64Encode(text) {
  155. return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(text));
  156. }
  157. export function base64Decode(text) {
  158. return CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(text));
  159. }
  160. export function urljoin(base, url) {
  161. base = base || '';
  162. url = url || '';
  163. let baseU = new Uri(base.trim().rstrip('/'));
  164. url = url.trim().rstrip('/');
  165. let u = undefined;
  166. if (url.startsWith('http://') || url.startsWith('https://')) {
  167. u = new Uri(url);
  168. } else if (url.startsWith('://')) {
  169. u = new Uri(baseU.protocol() + url);
  170. } else if (url.startsWith('//')) {
  171. u = new Uri(baseU.protocol() + ':' + url);
  172. } else {
  173. u = new Uri(baseU.protocol() + '://' + baseU.host() + (baseU.port() ? ':' + baseU.port() : '') + '/' + url);
  174. }
  175. if ((!u.path() || u.path().trim().length === 0) && baseU.path())
  176. u.path(baseU.path());
  177. if (!u.query() && baseU.query())
  178. u.query(baseU.query());
  179. return u.toString();
  180. }
  181. export function md5(src) {
  182. return CryptoJS.MD5(src).toString();
  183. }
  184. const DOM_CHECK_ATTR = /(url|src|href|data-original|data-src)$/;
  185. const SELECT_REGEX = /:eq|:lt|:gt|#/g;
  186. const SELECT_REGEX_A = /:eq|:lt|:gt/g;
  187. export function pdfh(html, parse, base_url) {
  188. if (!parse || !parse.trim()) {
  189. return ''
  190. }
  191. let eleFind = typeof html === 'object';
  192. let option = undefined;
  193. if (eleFind && parse.startsWith('body&&')) {
  194. parse = parse.substr(6);
  195. if (parse.indexOf('&&') < 0) {
  196. option = parse.trim();
  197. parse = '*=*';
  198. }
  199. }
  200. if (parse.indexOf('&&') > -1) {
  201. let sp = parse.split('&&');
  202. option = sp[sp.length - 1];
  203. sp.splice(sp.length - 1);
  204. if (sp.length > 1) {
  205. for (let i in sp) {
  206. if (!SELECT_REGEX.test(sp[i])) {
  207. sp[i] = sp[i] + ':eq(0)';
  208. }
  209. }
  210. } else {
  211. if (!SELECT_REGEX.test(sp[0])) {
  212. sp[0] = sp[0] + ':eq(0)';
  213. }
  214. }
  215. parse = sp.join(' ');
  216. }
  217. let result = '';
  218. const $ = eleFind ? html.rr : cheerio.load(html);
  219. let ret = eleFind ? ((parse === '*=*' || $(html.ele).is(parse)) ? html.ele : $(html.ele).find(parse)) : $(parse);
  220. if (option) {
  221. if (option === 'Text') {
  222. result = $(ret).text();
  223. }
  224. else if (option === 'Html') {
  225. result = $(ret).html();
  226. }
  227. else {
  228. result = $(ret).attr(option);
  229. }
  230. if (result && base_url && DOM_CHECK_ATTR.test(option)) {
  231. if (/http/.test(result)) {
  232. result = result.substr(result.indexOf('http'));
  233. } else {
  234. result = urljoin(base_url, result)
  235. }
  236. }
  237. } else {
  238. result = $(ret).toString();
  239. }
  240. return result;
  241. }
  242. export function pdfa(html, parse) {
  243. if (!parse || !parse.trim()) {
  244. return [];
  245. }
  246. let eleFind = typeof html === 'object';
  247. if (parse.indexOf('&&') > -1) {
  248. let sp = parse.split('&&');
  249. for (let i in sp) {
  250. if (!SELECT_REGEX_A.test(sp[i]) && i < sp.length - 1) {
  251. sp[i] = sp[i] + ':eq(0)';
  252. }
  253. }
  254. parse = sp.join(' ');
  255. }
  256. const $ = eleFind ? html.rr : cheerio.load(html);
  257. let ret = eleFind ? ($(html.ele).is(parse) ? html.ele : $(html.ele).find(parse)) : $(parse);
  258. let result = [];
  259. if (ret) {
  260. ret.each(function (idx, ele) {
  261. result.push({ rr: $, ele: ele });
  262. });
  263. }
  264. return result;
  265. }
  266. globalThis.randIndex = randIndex;
  267. globalThis.randomStr = randomStr;
  268. globalThis.urljoin = urljoin;
  269. globalThis.joinUrl = urljoin;
  270. globalThis.urljoin2 = urljoin;
  271. globalThis.是否正版 = 是否正版;
  272. globalThis.urlDeal = urlDeal;
  273. globalThis.urlencode = urlencode;
  274. globalThis.encodeUrl = urlencode;
  275. globalThis.base64Encode = base64Encode;
  276. globalThis.base64Decode = base64Decode;
  277. globalThis.pdfa = pdfa;
  278. globalThis.pdfh = pdfh;
  279. globalThis.md5 = md5;