Includes : Persist BETA

Persist Function

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/7144/29377/Includes%20%3A%20Persist%20BETA.js

  1. // ==UserScript==
  2. // @name Includes : Persist BETA
  3. // @namespace http://gm.wesley.eti.br
  4. // @description Persist Function
  5. // @author w35l3y
  6. // @email w35l3y@brasnet.org
  7. // @copyright 2013+, w35l3y (http://gm.wesley.eti.br)
  8. // @license GNU GPL
  9. // @homepage http://gm.wesley.eti.br
  10. // @version 1.0.1.0
  11. // @language en
  12. // @include nowhere
  13. // @exclude *
  14. // @require https://gf.qytechs.cn/scripts/7145-includes-xpath/code/Includes%20:%20XPath.js?version=29376
  15. // ==/UserScript==
  16.  
  17. /**************************************************************************
  18.  
  19. This program is free software: you can redistribute it and/or modify
  20. it under the terms of the GNU General Public License as published by
  21. the Free Software Foundation, either version 3 of the License, or
  22. (at your option) any later version.
  23.  
  24. This program is distributed in the hope that it will be useful,
  25. but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. GNU General Public License for more details.
  28.  
  29. You should have received a copy of the GNU General Public License
  30. along with this program. If not, see <http://www.gnu.org/licenses/>.
  31.  
  32. **************************************************************************/
  33.  
  34. var Persist = function (s) {
  35. var service = Persist.services[s];
  36.  
  37. this.request = function (obj) {
  38. var k,
  39. xthis = this,
  40. params = function (def) {
  41. var list = {};
  42.  
  43. this.add = function (vars) {
  44. if (vars) {
  45. if (vars instanceof unsafeWindow.HTMLCollection) {
  46. vars = Array.prototype.slice.apply(vars);
  47. }
  48.  
  49. if (typeof vars == "object") {
  50. for (var k in vars) {
  51. var e = vars[k];
  52.  
  53. if (typeof e == "object" && (!/^(?:radio|checkbox)$/i.test(e.type) || e.checked)) {
  54. var n = e.name || k;
  55.  
  56. if (e.checked && /^checkbox$/i.test(e.type)) {
  57. if (n in list) {
  58. list[n].push(e.value);
  59. } else {
  60. list[n] = [e.value];
  61. }
  62. } else if ("value" in e) {
  63. list[n] = e.value;
  64. } else {
  65. list[n] = e;
  66. }
  67. } else {
  68. list[k] = e;
  69. }
  70. }
  71. } else {
  72. list = vars;
  73. }
  74. }
  75. };
  76. this.toString = function() {
  77. if (typeof list == "object") {
  78. var data = "";
  79. for (var key in list) {
  80. if (list[key] instanceof Array) {
  81. var keyarr = key.replace(/^\s+|\s+$/g, "");
  82. if (!/\[\w*\]$/.test(key)) keyarr += "[]";
  83.  
  84. for (var k in list[key]) {
  85. var v = list[key][k];
  86. data += "&" + encodeURIComponent(keyarr) + "=" + encodeURIComponent(v);
  87. }
  88. } else {
  89. data += "&" + encodeURIComponent(key) + "=" + encodeURIComponent(list[key]);
  90. }
  91. }
  92.  
  93. return data.substr(1);
  94. } else {
  95. return list;
  96. }
  97. };
  98. },
  99. data = new params();
  100.  
  101. data.add(obj.pdata);
  102. data.add(obj.adata);
  103. data.add(obj.data);
  104. data.add(obj.odata);
  105. if (typeof obj.onload != "function") {
  106. obj.onload = function (obj) {
  107. console.log(["request.load", obj]);
  108. };
  109. }
  110.  
  111. if (typeof obj.onerror != "function") {
  112. obj.onerror = function (obj) {
  113. console.log(["request.error", obj]);
  114. };
  115. }
  116.  
  117. var update = function (obj, e) {
  118. obj.response = {
  119. raw : function () {return e;},
  120. text : function () {return e.responseText;},
  121. json : function () {
  122. try {
  123. return JSON.parse(e.responseText);
  124. } catch (x) {
  125. return eval("(" + e.responseText + ")");
  126. }
  127. },
  128. doc : function () {
  129. try {
  130. return new DOMParser().parseFromString(e.responseText, /^Content-Type: ([\w/]+)$/mi.test(e.responseHeaders) && RegExp.$1 || "text/html");
  131. } catch (x) {
  132. var doc = document.implementation.createHTMLDocument("");
  133. doc.documentElement.innerHTML = e.responseText;
  134.  
  135. return doc;
  136. }
  137. },
  138. };
  139. obj.value = e.responseText;
  140. },
  141. params = {
  142. url : obj.url,
  143. method : obj.method,
  144. onload : function (e) {
  145. update(obj, e);
  146.  
  147. obj[(/^2/.test(e.status)?"onload":"onerror")].apply(xthis, [obj]);
  148. },
  149. onerror : function (e) {
  150. update(obj, e);
  151.  
  152. obj.onerror.apply(xthis, [obj]);
  153. }
  154. },
  155. sdata = data.toString();
  156.  
  157. if (/^post$/i.test(obj.method)) {
  158. var p = {
  159. headers : {
  160. "Content-Type" : "application/x-www-form-urlencoded"
  161. },
  162. data : sdata,
  163. };
  164.  
  165. console.log(sdata);
  166.  
  167. for (k in p) {
  168. params[k] = p[k];
  169. }
  170. } else if (sdata) {
  171. params.url += "?" + sdata;
  172.  
  173. }
  174. if (typeof obj.headers == "object")
  175. for (k in obj.headers) {
  176. params.headers[k] = obj.headers[k];
  177. }
  178.  
  179. return GM_xmlhttpRequest(params);
  180. };
  181.  
  182. for (var k in service) {
  183. this[k] = service[k];
  184. }
  185. };
  186.  
  187. Persist.services = {
  188. LOCAL : {
  189. write : function (obj) {
  190. var label_keys = "persist_keys-" + obj.service,
  191. label_data = "persist_data-" + obj.service,
  192. mapping = JSON.parse(GM_getValue(label_keys, "{}")),
  193. data = JSON.parse(GM_getValue(label_data, "[]")),
  194. key = obj.key;
  195.  
  196. if (key in mapping) {
  197. key = mapping[obj.key];
  198. }
  199.  
  200. if (key in data) {
  201. var tv = typeof obj.value,
  202. td = typeof data[key];
  203.  
  204. if ((td != tv) && (td == "object" || tv == "object")) {
  205. throw ["Incompatible types ", td, tv].toString();
  206. } else {
  207. switch (td) {
  208. case "string":
  209. if (-1 == obj.mode) { // prepend
  210. data[key] = obj.value + data[key];
  211. } else if (1 == obj.mode) { // append
  212. data[key] += obj.value;
  213. } else { // overwrite (default)
  214. data[key] = obj.value;
  215. }
  216. break;
  217. case "boolean":
  218. if (-1 == obj.mode) { // unused
  219. throw ["Reserved action"].toString();
  220. } else if (1 == obj.mode) { // toggle
  221. data[key] = !data[key];
  222. } else { // overwrite (default)
  223. data[key] = !!value;
  224. }
  225. break;
  226. case "number":
  227. var value = Number(obj.value);
  228.  
  229. if (-1 == obj.mode) { // subtract
  230. data[key] -= value;
  231. } else if (1 == obj.mode) { // add
  232. data[key] += value;
  233. } else { // overwrite (default)
  234. data[key] = value;
  235. }
  236. break;
  237. case "object":
  238. if (-1 == obj.mode) { // prepend
  239. for (var k in data[key]) {
  240. obj.value[k] = data[key][k];
  241. }
  242. data[key] = obj.value;
  243. } else if (1 == obj.mode) { // append
  244. for (var k in obj.value) {
  245. data[key][k] = obj.value[k];
  246. }
  247. } else { // overwrite (default)
  248. data[key] = obj.value;
  249. }
  250. break;
  251. default:
  252. throw ["Unsupported type " + td, data[key], key].toString();
  253. }
  254.  
  255. obj.value = data[key];
  256. }
  257. } else {
  258. var tkey = data.push(obj.value);
  259. if (--tkey != key) {
  260. if ("key" in obj) {
  261. if (key in mapping) {
  262. console.log(["Wrong mapping... ", tkey, key]);
  263. }
  264.  
  265. mapping[key] = tkey;
  266. obj.key = key;
  267.  
  268. GM_setValue(label_keys, JSON.stringify(mapping));
  269. } else {
  270. obj.key = tkey;
  271. }
  272. }
  273. }
  274.  
  275. GM_setValue(label_data, JSON.stringify(data));
  276.  
  277. if (typeof obj.onload != "function") {
  278. obj.onload = function (obj) {
  279. console.log(["write.load", obj]);
  280. };
  281. }
  282.  
  283. return obj.onload.apply(this, [obj]);
  284. },
  285. delete : function (obj) {
  286. throw ["Not implemented"].toString();
  287. },
  288. read : function (obj) {
  289. var mapping = JSON.parse(GM_getValue("persist_keys-" + obj.service, "{}")),
  290. key = obj.key;
  291.  
  292. if (key in mapping) {
  293. key = mapping[key];
  294. }
  295.  
  296. obj.value = JSON.parse(GM_getValue("persist_data-" + obj.service, "[]"))[key];
  297.  
  298. if (typeof obj.onload != "function") {
  299. obj.onload = function (obj) {
  300. console.log(["read.load", obj]);
  301. };
  302. }
  303.  
  304. return obj.onload.apply(this, [obj]);
  305. },
  306. },
  307. PASTEBIN : {
  308. write : function (obj) {
  309. var execute = function (x) {
  310. var onload = x.onload,
  311. value = x.value,
  312. xthis = this,
  313. p = {
  314. url : "http://pastebin.com/api/api_post.php",
  315. method : "post",
  316. adata : JSON.parse(GM_getValue("pastebin_adata", JSON.stringify({
  317. // api_dev_key : "", // required
  318. // api_user_key : "",
  319. }))),
  320. pdata : {
  321. api_paste_format : "text",
  322. api_paste_private : "1",
  323. api_paste_expire_date : "N",
  324. api_paste_code : x.value, // required
  325. },
  326. odata : {
  327. api_paste_key : obj.key,
  328. api_option : "paste"
  329. },
  330. onload : function (y) {
  331. if (/^https?:\/\//i.test(y.value) && /\w+$/.test(y.value)) {
  332. var key = x.key;
  333. x.key = RegExp["$&"];
  334.  
  335. x.onload = onload;
  336. x.value = value;
  337.  
  338. x.onload.apply(xthis, [x]);
  339.  
  340. if (x.read) {
  341. /* It is implemented that way because currently there isn't an EDIT option via API */
  342. y.key = key;
  343. y.onload = function (z) {
  344. console.log(["delete.load", z]);
  345. };
  346. y.onerror = function (z) {
  347. console.log(["delete.error", z]);
  348. };
  349.  
  350. xthis.delete.apply(xthis, [y]);
  351. }
  352. } else {
  353. x.onerror.apply(xthis, [x]);
  354. }
  355. }
  356. };
  357. for (var i in p) {
  358. x[i] = p[i];
  359. }
  360. return this.request(x);
  361. };
  362.  
  363. if (1 == Math.abs(obj.mode)) { // prepend or append
  364. var value = obj.value,
  365. onload = obj.onload,
  366. xthis = this;
  367.  
  368. obj.onload = function (x) {
  369. obj.onload = onload;
  370.  
  371. if (-1 == obj.mode) { // prepend
  372. obj.value = value + obj.value;
  373. } else { // append
  374. obj.value += value;
  375. }
  376. return execute.apply(xthis, [obj]);
  377. };
  378.  
  379. return this.read.apply(this, [obj]);
  380. } else {
  381. return execute.apply(this, [obj]);
  382. }
  383. },
  384. read : function (obj) {
  385. if ("key" in obj) {
  386. var onload = obj.onload,
  387. xthis = this,
  388. p = {
  389. read : false,
  390. url : "http://pastebin.com/download.php",
  391. method : "get",
  392. adata : {
  393. i : obj.key
  394. },
  395. onload : function (x) {
  396. obj.onload = onload;
  397.  
  398. if (x.response.raw().finalUrl.indexOf(x.url)) {
  399. obj.value = "";
  400.  
  401. obj[typeof obj.onwarn == "function"?"onwarn":"onload"].apply(this, [obj]);
  402. } else {
  403. obj.read = true;
  404.  
  405. obj.onload.apply(xthis, [obj]);
  406. }
  407. }
  408. };
  409. for (var i in p) {
  410. obj[i] = p[i];
  411. }
  412.  
  413. return this.request(obj);
  414. } else {
  415. obj.value = "";
  416. obj.read = false;
  417.  
  418. obj[typeof obj.onwarn == "function"?"onwarn":"onload"].apply(this, [obj]);
  419. }
  420. },
  421. delete : function (obj) {
  422. var onload = obj.onload,
  423. value = obj.value,
  424. xthis = this,
  425. p = {
  426. url : "http://pastebin.com/api/api_post.php",
  427. method : "post",
  428. adata : JSON.parse(GM_getValue("pastebin_adata", JSON.stringify({
  429. // api_dev_key : "", // required
  430. // api_user_key : "",
  431. }))),
  432. pdata : {},
  433. odata : {
  434. api_paste_key : obj.key,
  435. api_option : "delete"
  436. },
  437. onload : function (x) {
  438. if (/^Paste Removed$/i.test(x.value)) {
  439. obj.onload = onload;
  440. obj.value = value;
  441.  
  442. obj.onload.apply(xthis, [obj]);
  443. } else {
  444. obj.onerror.apply(xthis, [obj]);
  445. }
  446. }
  447. };
  448. for (var i in p) {
  449. obj[i] = p[i];
  450. }
  451. return this.request(obj);
  452. },
  453. },
  454. PASTEBIN2 : {
  455. write : function (obj) {
  456. var ovalue = obj.value,
  457. xthis = this,
  458. execute = function (x) {
  459. var onload = x.onload,
  460. nvalue = x.value,
  461. p = {
  462. method : "post",
  463. adata : JSON.parse(GM_getValue("pastebin2_adata", JSON.stringify({
  464. // paste_private : "1",
  465. }))),
  466. pdata : {
  467. paste_format : "1",
  468. paste_private : "2",
  469. paste_expire_date : "N",
  470. paste_code : x.value, // required
  471. },
  472. odata : {
  473. submit : "Submit",
  474. submit_hidden : "submit_hidden",
  475. item_key : obj.key,
  476. post_key : obj.key,
  477. },
  478. onload : function (y) {
  479. var doc = y.response.doc(),
  480. url = y.response.raw().finalUrl;
  481.  
  482. if (/warning\.php\?p=(\d+)/i.test(url)) {
  483. x.code = RegExp.$1 - 1;
  484. x.value = [
  485. "You have reached your limit of [10] pastes per 24 hours.",
  486. "You have reached your limit of [20] pastes per 24 hours.",
  487. "You have reached your limit of [250] pastes per 24 hours.",
  488. ][x.code] || xpath("string(id('content_left')/div[2])", doc) || "Unknown error (WARN " + x.code + ")";
  489.  
  490. x.onerror.apply(xthis, [x]);
  491. } else if ((/^https?:/i.test(url)) && (/\/(\w+)$/.test(url))) {
  492. x.key = RegExp.$1;
  493.  
  494. if (xpath("string(.//text()[contains(., 'Pastebin.com is under heavy load right now')])", doc)) {
  495. x.onerror.apply(xthis, [x]);
  496. } else {
  497. if (xpath("id('siimage')", doc)[0]) {
  498. x.value = nvalue;
  499. GM_log(ovalue);
  500. alert("A new window will be opened. You must fill the captcha correctly, otherwise you will lose your data and a new paste will be created next time.");
  501. GM_openInTab(url);
  502. } else {
  503. var code = xpath("id('paste_code')", doc)[0];
  504. x.value = code && code.textContent || "";
  505. }
  506. x.onload = onload;
  507. x.onload.apply(xthis, [x]);
  508. }
  509. } else {
  510. x.value = [
  511. "You have exceeded the maximum file size of [500] kilobytes per paste.",
  512. "You cannot create an empty paste.",
  513. "You have reached the maximum number of [25] unlisted pastes.",
  514. "You have reached the maximum number of [10] private pastes.",
  515. ][/index\.php\?e=(\d+)/.test(url) && (x.code = RegExp.$1 - 1)] || xpath("string(id('notice'))", doc) || "Unknown error (ERROR " + x.code + ")";
  516.  
  517. x.onerror.apply(xthis, [x]);
  518. }
  519. }
  520. };
  521. for (var i in p) {
  522. x[i] = p[i];
  523. }
  524.  
  525. return this.request(x);
  526. };
  527.  
  528. if (1 == Math.abs(obj.mode)) { // prepend or append
  529. var onload = obj.onload;
  530.  
  531. if ("" != ovalue) {
  532. obj.onload = function (x) {
  533. obj.onload = onload;
  534.  
  535. if (-1 == obj.mode) { // prepend
  536. obj.value = ovalue + obj.value;
  537. } else { // append
  538. obj.value += ovalue;
  539. }
  540. return execute.apply(xthis, [obj]);
  541. };
  542. }
  543.  
  544. return xthis.read.apply(xthis, [obj]);
  545. } else {
  546. return execute.apply(xthis, [obj]);
  547. }
  548. },
  549. read : function (obj) {
  550. var xthis = this;
  551.  
  552. if ("key" in obj && obj.key) {
  553. var onload = obj.onload,
  554. p = {
  555. read : false,
  556. url : "http://pastebin.com/download.php",
  557. method : "get",
  558. adata : {
  559. i : obj.key
  560. },
  561. onload : function (x) {
  562. var url = x.response.raw().finalUrl;
  563.  
  564. x.onload = onload;
  565.  
  566. if (url.indexOf(x.url)) { // Unknown Paste ID
  567. x.value = "";
  568. x.url = "http://pastebin.com/post.php";
  569. x.key = "";
  570.  
  571. x[typeof x.onwarn == "function"?"onwarn":"onload"].apply(this, [x]);
  572. } else if (~x.value.indexOf("this is a private paste. If this is your private paste")) { // may occur false positive
  573. x.value = "This is a private paste (#"+x.key+"). You must login to Pastebin first.";
  574. x.onerror.apply(xthis, [x]);
  575. } else if (~x.value.indexOf("Pastebin.com is under heavy load right now")) {
  576. x.value = "Pastebin.com is under heavy load right now.";
  577. x.onerror.apply(xthis, [x]);
  578. } else {
  579. x.read = true;
  580. x.url = "http://pastebin.com/edit.php";
  581.  
  582. x.onload.apply(xthis, [x]);
  583. }
  584. }
  585. };
  586. for (var i in p) {
  587. obj[i] = p[i];
  588. }
  589.  
  590. return this.request(obj);
  591. } else {
  592. obj.read = false;
  593. obj.url = "http://pastebin.com/post.php";
  594. obj.key = "";
  595.  
  596. this.request({
  597. url : "http://pastebin.com/",
  598. method : "get",
  599. onload : function (c) {
  600. if (xpath("boolean(id('header_bottom')//a[contains(@href, '/logout')])", c.response.doc())) {
  601. obj.value = "";
  602.  
  603. obj[typeof obj.onwarn == "function"?"onwarn":"onload"].apply(xthis, [obj]);
  604. } else {
  605. obj.value = "You must login to Pastebin first.";
  606.  
  607. obj.onerror.apply(xthis, [obj]);
  608. }
  609. },
  610. onerror : function (c) {
  611. c.value = xpath("string(id('content_left')/div[1])", c.response.doc());
  612.  
  613. obj.onerror(c);
  614. },
  615. });
  616. }
  617. },
  618. delete : function (obj) {
  619. var onload = obj.onload,
  620. value = obj.value,
  621. xthis = this,
  622. p = {
  623. url : "http://pastebin.com/delete.php",
  624. method : "get",
  625. adata : {},
  626. pdata : {},
  627. odata : {
  628. i : obj.key,
  629. r : "/" + obj.key
  630. },
  631. onload : function (x) {
  632. if (/^Paste Removed$/i.test(x.value)) {
  633. obj.onload = onload;
  634. obj.value = value;
  635.  
  636. obj.onload.apply(xthis, [obj]);
  637. } else {
  638. obj.onerror.apply(xthis, [obj]);
  639. }
  640. }
  641. };
  642. for (var i in p) {
  643. obj[i] = p[i];
  644. }
  645. return this.request(obj);
  646. }
  647. }
  648. };
  649.  
  650. Persist.write = function (obj) {
  651. var p = new Persist(obj.service);
  652.  
  653. return p.write.apply(p, [obj]);
  654. };
  655.  
  656. Persist.delete = function (obj) {
  657. var p = new Persist(obj.service);
  658.  
  659. return p.delete.apply(p, [obj]);
  660. };
  661.  
  662. Persist.read = function (obj) {
  663. var p = new Persist(obj.service);
  664.  
  665. return p.read.apply(p, [obj]);
  666. };

QingJ © 2025

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