script-statistics

脚本高级统计

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/461573/1162621/script-statistics.js

  1. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  2. function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, defineProperty = Object.defineProperty || function (obj, key, desc) { obj[key] = desc.value; }, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return defineProperty(generator, "_invoke", { value: makeInvokeMethod(innerFn, self, context) }), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; defineProperty(this, "_invoke", { value: function value(method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; } function maybeInvokeDelegate(delegate, context) { var methodName = context.method, method = delegate.iterator[methodName]; if (undefined === method) return context.delegate = null, "throw" === methodName && delegate.iterator.return && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method) || "return" !== methodName && (context.method = "throw", context.arg = new TypeError("The iterator does not provide a '" + methodName + "' method")), ContinueSentinel; var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, defineProperty(Gp, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), defineProperty(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (val) { var object = Object(val), keys = []; for (var key in object) keys.push(key); return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, catch: function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
  3. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  4. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  5. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  7. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  8. function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  9. function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
  10. function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
  11. var SC_Statistic = /*#__PURE__*/function () {
  12. function SC_Statistic() {
  13. var option = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  14. _classCallCheck(this, SC_Statistic);
  15. _defineProperty(this, "baseURL", "");
  16. _defineProperty(this, "intervalHanlde", undefined);
  17. _defineProperty(this, "visitHash", undefined);
  18. _defineProperty(this, "session_id", undefined);
  19. _defineProperty(this, "key", undefined);
  20. _defineProperty(this, "visit_time", undefined);
  21. _defineProperty(this, "install_page", "");
  22. _defineProperty(this, "operation_page", "");
  23. _defineProperty(this, "version", undefined);
  24. _defineProperty(this, "iframe", false);
  25. _defineProperty(this, "postData", undefined);
  26. _defineProperty(this, "enableGM", true);
  27. _defineProperty(this, "enableXML", true);
  28. _defineProperty(this, "whitelist", []);
  29. var _option$intervalTime = option.intervalTime,
  30. intervalTime = _option$intervalTime === void 0 ? 1000 * 60 * 2 : _option$intervalTime,
  31. _option$customHash = option.customHash,
  32. customHash = _option$customHash === void 0 ? undefined : _option$customHash,
  33. _option$key = option.key,
  34. key = _option$key === void 0 ? undefined : _option$key,
  35. _option$banFrame = option.banFrame,
  36. banFrame = _option$banFrame === void 0 ? false : _option$banFrame,
  37. _option$enableGM = option.enableGM,
  38. enableGM = _option$enableGM === void 0 ? true : _option$enableGM,
  39. _option$enableXML = option.enableXML,
  40. enableXML = _option$enableXML === void 0 ? true : _option$enableXML,
  41. _option$baseURL = option.baseURL,
  42. baseURL = _option$baseURL === void 0 ? "https://scriptcat.org/api/v2/statistics/collect" : _option$baseURL,
  43. _option$whitelist = option.whitelist,
  44. whitelist = _option$whitelist === void 0 ? [] : _option$whitelist;
  45. this.whitelist = Array.isArray(whitelist) ? whitelist : [whitelist];
  46. this.baseURL = baseURL;
  47. this.enableGM = enableGM;
  48. this.enableXML = enableXML;
  49. this.session_id = this.generateUUID();
  50. this.key = key;
  51. this.customHash = customHash;
  52. this.visit_time = this.getUnixTime();
  53. this.install_page = GM_info.scriptUpdateURL || "";
  54. this.operation_page = window.location.href;
  55. this.iframe = self != top;
  56. this.version = GM_info.script.version || "0.0.0";
  57. if (banFrame && this.iframe) {
  58. return;
  59. }
  60. this.init({
  61. intervalTime: intervalTime
  62. });
  63. }
  64. _createClass(SC_Statistic, [{
  65. key: "init",
  66. value: function () {
  67. var _init = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
  68. var _this = this;
  69. var intervalTime;
  70. return _regeneratorRuntime().wrap(function _callee$(_context) {
  71. while (1) switch (_context.prev = _context.next) {
  72. case 0:
  73. intervalTime = _ref.intervalTime;
  74. if (this.key === undefined) {
  75. console.log("SC_Statistic: the key params is required");
  76. }
  77. _context.next = 4;
  78. return this.getNetworkWhite();
  79. case 4:
  80. this.developHelper();
  81. if (this.validWhiteList()) {
  82. _context.next = 7;
  83. break;
  84. }
  85. return _context.abrupt("return");
  86. case 7:
  87. this.submitMessage();
  88. if (intervalTime !== undefined) {
  89. this.intervalHanlde = setInterval(this.submitMessage.bind(this), intervalTime);
  90. }
  91. if (navigator.sendBeacon) {
  92. window.addEventListener("beforeunload", function () {
  93. if (_this.postData) {
  94. var fd = new FormData();
  95. var mergeObj = {
  96. duration: _this.getUnixTime() - _this.visit_time
  97. };
  98. var contentObj = _this.postData;
  99. for (var key in contentObj) {
  100. if (mergeObj[key]) {
  101. fd.append(key, mergeObj[key]);
  102. } else {
  103. fd.append(key, contentObj[key]);
  104. }
  105. }
  106. fd.append("exit_time", _this.getUnixTime());
  107. navigator.sendBeacon(_this.baseURL, fd);
  108. }
  109. }, false);
  110. }
  111. case 10:
  112. case "end":
  113. return _context.stop();
  114. }
  115. }, _callee, this);
  116. }));
  117. function init(_x) {
  118. return _init.apply(this, arguments);
  119. }
  120. return init;
  121. }()
  122. }, {
  123. key: "getNetworkWhite",
  124. value: function () {
  125. var _getNetworkWhite = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
  126. var netWorkKey, netWorkValue, isRun, currentDate, lastDate, timeDiff, result, response;
  127. return _regeneratorRuntime().wrap(function _callee2$(_context2) {
  128. while (1) switch (_context2.prev = _context2.next) {
  129. case 0:
  130. if (!(typeof GM_getValue !== "undefined" && typeof GM_setValue !== "undefined" && this.whitelist.length === 0)) {
  131. _context2.next = 10;
  132. break;
  133. }
  134. netWorkKey = "__NetworkWhiteData";
  135. netWorkValue = GM_getValue(netWorkKey);
  136. isRun = false;
  137. if (netWorkValue === undefined) {
  138. isRun = true;
  139. } else {
  140. try {
  141. netWorkValue = JSON.parse(netWorkValue);
  142. currentDate = new Date();
  143. lastDate = new Date(netWorkValue.time);
  144. timeDiff = currentDate - lastDate;
  145. if (timeDiff > 3600000) {
  146. isRun = true;
  147. } else {
  148. this.whitelist = netWorkValue.whitelist;
  149. }
  150. } catch (error) {
  151. isRun = true;
  152. }
  153. }
  154. if (!isRun) {
  155. _context2.next = 10;
  156. break;
  157. }
  158. _context2.next = 8;
  159. return this.UNION_Post({
  160. url: this.baseURL + "/whitelist",
  161. data: {
  162. statistics_key: this.key
  163. }
  164. });
  165. case 8:
  166. result = _context2.sent;
  167. if (result.status) {
  168. response = result.data;
  169. if (response && response.msg === "success" && response.code === 0) {
  170. this.whitelist = response.data.whitelist;
  171. GM_setValue(netWorkKey, JSON.stringify({
  172. time: Date.now(),
  173. whitelist: this.whitelist
  174. }));
  175. }
  176. }
  177. case 10:
  178. case "end":
  179. return _context2.stop();
  180. }
  181. }, _callee2, this);
  182. }));
  183. function getNetworkWhite() {
  184. return _getNetworkWhite.apply(this, arguments);
  185. }
  186. return getNetworkWhite;
  187. }()
  188. }, {
  189. key: "developHelper",
  190. value: function developHelper() {
  191. if (!this.enableXML && !this.enableGM) {
  192. console.warn("Log is no network handle,please enable any method or enable GM_xmlhttpRequest.");
  193. }
  194. if (this.whitelist.length === 0) {
  195. console.log("SC_Statistic: whitelist is no data,please input statistic website list");
  196. }
  197. }
  198. }, {
  199. key: "validWhiteList",
  200. value: function validWhiteList() {
  201. var host = window.location.host;
  202. var whitelist = this.whitelist;
  203. for (var index = 0; index < whitelist.length; index++) {
  204. var whiteItem = whitelist[index];
  205. if (whiteItem.split(".").length < 2) {
  206. console.log("SC_Statistic: whitelist item must have [.] char");
  207. continue;
  208. }
  209. if (host.includes(whiteItem)) {
  210. return true;
  211. }
  212. }
  213. return false;
  214. }
  215. }, {
  216. key: "closeintervalTime",
  217. value: function closeintervalTime() {
  218. clearInterval(this.intervalHanlde);
  219. }
  220. }, {
  221. key: "GM_Post",
  222. value: function GM_Post(_ref2) {
  223. var _this2 = this;
  224. var url = _ref2.url,
  225. data = _ref2.data;
  226. return new Promise(function (resolve) {
  227. var parseResponse = _this2.parseResponse;
  228. if (typeof GM_xmlhttpRequest !== "undefined") {
  229. GM_xmlhttpRequest({
  230. url: url,
  231. method: "POST",
  232. data: JSON.stringify(data),
  233. headers: {
  234. "Content-type": "application/json"
  235. },
  236. onload: function onload(xhr) {
  237. resolve({
  238. status: true,
  239. data: parseResponse(xhr.responseText)
  240. });
  241. },
  242. onerror: function onerror(xhr) {
  243. resolve({
  244. status: false
  245. });
  246. },
  247. ontimeout: function ontimeout(xhr) {
  248. resolve({
  249. status: false
  250. });
  251. }
  252. });
  253. } else {
  254. resolve({
  255. status: false
  256. });
  257. }
  258. });
  259. }
  260. }, {
  261. key: "XML_Post",
  262. value: function XML_Post(_ref3) {
  263. var _this3 = this;
  264. var url = _ref3.url,
  265. data = _ref3.data;
  266. return new Promise(function (resolve) {
  267. var parseResponse = _this3.parseResponse;
  268. try {
  269. var xmlhttp = new XMLHttpRequest();
  270. xmlhttp.open("POST", url, true);
  271. xmlhttp.setRequestHeader("Content-type", "application/json");
  272. xmlhttp.onreadystatechange = function () {
  273. if (this.readyState == XMLHttpRequest.DONE) {
  274. if (this.status !== 200) {
  275. resolve({
  276. status: false
  277. });
  278. } else {
  279. resolve({
  280. status: true,
  281. data: parseResponse(this.response)
  282. });
  283. }
  284. }
  285. };
  286. xmlhttp.send(JSON.stringify(data));
  287. } catch (error) {
  288. resolve({
  289. status: false
  290. });
  291. }
  292. });
  293. }
  294. }, {
  295. key: "parseResponse",
  296. value: function parseResponse(text) {
  297. try {
  298. return JSON.parse(text);
  299. } catch (error) {
  300. return text;
  301. }
  302. }
  303. }, {
  304. key: "UNION_Post",
  305. value: function () {
  306. var _UNION_Post = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(config) {
  307. var result, _result;
  308. return _regeneratorRuntime().wrap(function _callee3$(_context3) {
  309. while (1) switch (_context3.prev = _context3.next) {
  310. case 0:
  311. if (!this.enableXML) {
  312. _context3.next = 6;
  313. break;
  314. }
  315. _context3.next = 3;
  316. return this.XML_Post(config);
  317. case 3:
  318. result = _context3.sent;
  319. if (!result.status) {
  320. _context3.next = 6;
  321. break;
  322. }
  323. return _context3.abrupt("return", result);
  324. case 6:
  325. if (!this.enableGM) {
  326. _context3.next = 12;
  327. break;
  328. }
  329. _context3.next = 9;
  330. return this.GM_Post(config);
  331. case 9:
  332. _result = _context3.sent;
  333. if (!_result.status) {
  334. _context3.next = 12;
  335. break;
  336. }
  337. return _context3.abrupt("return", _result);
  338. case 12:
  339. return _context3.abrupt("return", {
  340. status: false
  341. });
  342. case 13:
  343. case "end":
  344. return _context3.stop();
  345. }
  346. }, _callee3, this);
  347. }));
  348. function UNION_Post(_x2) {
  349. return _UNION_Post.apply(this, arguments);
  350. }
  351. return UNION_Post;
  352. }()
  353. }, {
  354. key: "flushContent",
  355. value: function () {
  356. var _flushContent = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
  357. var visitEntity, duration, hashContent;
  358. return _regeneratorRuntime().wrap(function _callee4$(_context4) {
  359. while (1) switch (_context4.prev = _context4.next) {
  360. case 0:
  361. _context4.next = 2;
  362. return this.getVisitHash();
  363. case 2:
  364. visitEntity = _context4.sent;
  365. duration = this.getUnixTime() - this.visit_time;
  366. hashContent = {
  367. visitor_id: visitEntity.hash,
  368. visitor_type: visitEntity.type,
  369. operation_page: this.operation_page,
  370. duration: duration,
  371. install_page: this.install_page,
  372. statistics_key: this.key,
  373. session_id: this.session_id,
  374. visit_time: this.visit_time,
  375. version: this.version
  376. };
  377. return _context4.abrupt("return", hashContent);
  378. case 6:
  379. case "end":
  380. return _context4.stop();
  381. }
  382. }, _callee4, this);
  383. }));
  384. function flushContent() {
  385. return _flushContent.apply(this, arguments);
  386. }
  387. return flushContent;
  388. }()
  389. }, {
  390. key: "submitMessage",
  391. value: function () {
  392. var _submitMessage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
  393. return _regeneratorRuntime().wrap(function _callee5$(_context5) {
  394. while (1) switch (_context5.prev = _context5.next) {
  395. case 0:
  396. _context5.prev = 0;
  397. _context5.next = 3;
  398. return this.flushContent();
  399. case 3:
  400. this.postData = _context5.sent;
  401. this.UNION_Post({
  402. url: this.baseURL + "",
  403. data: this.postData
  404. });
  405. _context5.next = 9;
  406. break;
  407. case 7:
  408. _context5.prev = 7;
  409. _context5.t0 = _context5["catch"](0);
  410. case 9:
  411. case "end":
  412. return _context5.stop();
  413. }
  414. }, _callee5, this, [[0, 7]]);
  415. }));
  416. function submitMessage() {
  417. return _submitMessage.apply(this, arguments);
  418. }
  419. return submitMessage;
  420. }()
  421. }, {
  422. key: "getVisitHash",
  423. value: function () {
  424. var _getVisitHash = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
  425. var saveVisit, customHasg, hash;
  426. return _regeneratorRuntime().wrap(function _callee6$(_context6) {
  427. while (1) switch (_context6.prev = _context6.next) {
  428. case 0:
  429. if (!(this.visitHash !== undefined)) {
  430. _context6.next = 2;
  431. break;
  432. }
  433. return _context6.abrupt("return", this.visitHash);
  434. case 2:
  435. saveVisit = {
  436. hash: "",
  437. type: 2
  438. };
  439. customHasg = this.customHash && this.customHash();
  440. if (customHasg !== undefined) {
  441. saveVisit = {
  442. hash: customHasg,
  443. type: 3
  444. };
  445. } else if (typeof GM_getValue !== "undefined" && typeof GM_setValue !== "undefined") {
  446. saveVisit = {
  447. hash: this.getGmHash(),
  448. type: 1
  449. };
  450. } else {
  451. hash = localStorage["__VisitUUID"];
  452. if (hash === undefined) {
  453. hash = this.generateUUID();
  454. localStorage["__VisitUUID"] = hash;
  455. }
  456. saveVisit.hash = hash;
  457. }
  458. return _context6.abrupt("return", this.visitHash = saveVisit);
  459. case 6:
  460. case "end":
  461. return _context6.stop();
  462. }
  463. }, _callee6, this);
  464. }));
  465. function getVisitHash() {
  466. return _getVisitHash.apply(this, arguments);
  467. }
  468. return getVisitHash;
  469. }()
  470. }, {
  471. key: "getGmHash",
  472. value: function getGmHash() {
  473. var hashResult = GM_getValue("__VisitUUID");
  474. if (hashResult === undefined) {
  475. hashResult = this.generateUUID();
  476. GM_setValue("__VisitUUID", hashResult);
  477. }
  478. return hashResult;
  479. }
  480. }, {
  481. key: "getUnixTime",
  482. value: function getUnixTime() {
  483. return Math.floor(Date.parse(new Date()) / 1000);
  484. }
  485. }, {
  486. key: "generateUUID",
  487. value: function generateUUID() {
  488. var d = Date.now();
  489. if (typeof performance !== "undefined" && typeof performance.now === "function") {
  490. d += performance.now();
  491. }
  492. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
  493. var r = (d + Math.random() * 16) % 16 | 0;
  494. d = Math.floor(d / 16);
  495. return (c === "x" ? r : r & 0x3 | 0x8).toString(16);
  496. });
  497. }
  498. }]);
  499. return SC_Statistic;
  500. }();