test3

nadirkitap isbn13 search

  1. // ==UserScript==
  2. // @name test3
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.7
  5. // @description nadirkitap isbn13 search
  6. // @author aek
  7. // @match https://www.desibona.com/*
  8. // @license MIT
  9. // @noframes
  10. // @icon https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://desibona.com&size=64
  11. // @grant GM.xmlHttpRequest
  12. // @grant GM_setValue
  13. // @grant GM_getValue
  14. // ==/UserScript==
  15. (function () {
  16. var sellerId = "25233";
  17. var description = `
  18. `;
  19. var description2 = `Türkiye'nin e-pazaryeri platformu.`;
  20.  
  21. description = description2;
  22. //description = undefined;
  23. if (location.href.startsWith("https://www.desibona.com/baston-bebek-arabasi-puset")) {
  24. var serializeForm = function () {
  25. return Array.from(new FormData(document.getElementsByName("ekle")[0]), function (field) {
  26. return field.map(encodeURIComponent).join('=');
  27. }).join('&');
  28. };
  29. var oldState;
  30. setTimeout(function() {
  31. oldState = serializeForm(document.getElementsByName("ekle")[0]);
  32. }, 1000);
  33. var oldState = serializeForm();
  34. console.log(oldState);
  35. window.addEventListener('beforeunload', function (e) {
  36. console.log(serializeForm(document.getElementsByName("ekle")[0]));
  37. if (oldState && oldState != serializeForm(document.getElementsByName("ekle")[0])) {
  38. e.returnValue = 'Emin misin?';
  39. }
  40. });
  41. var junks = document.querySelectorAll("p.a14");
  42. for (var index = 0; index < junks.length; index++) {
  43. var junk = junks[index];
  44. junk.parentElement.removeChild(junk);
  45. }
  46. junks = document.querySelectorAll("div[class='col-xs-12 col-md-6']");
  47. function removeJunk(junkPanel) {
  48. for (var i = 0; i < 6; i++) {
  49. junkPanel.children[3].parentNode.removeChild(junkPanel.children[3]);
  50. }
  51. }
  52. removeJunk(junks[0]);
  53. removeJunk(junks[1]);
  54. var list = document.querySelector(".member-list");
  55. list.innerHTML = "";
  56. var li = document.createElement("li");
  57. li.appendChild(document.getElementsByName("guzelciltli")[0].parentNode);
  58. list.appendChild(li);
  59. li = document.createElement("li");
  60. li.appendChild(document.getElementsByName("birincibaski")[0].parentNode);
  61. list.appendChild(li);
  62. li = document.createElement("li");
  63. li.appendChild(document.getElementsByName("imzali")[0].parentNode);
  64. list.appendChild(li);
  65. var junk = document.querySelector("span[style='font-size:10px;']");
  66. junk.parentElement.removeChild(junk);
  67. var nomargins = document.querySelectorAll("p.col-md-12.no-padding, .form-control.all-input");
  68. for (var index = 0; index < nomargins.length; index++) {
  69. nomargins[index].style.margin = 0;
  70. nomargins[index].style.padding = 0;
  71. }
  72. var q = document.querySelectorAll("div.margin-top10.col-md-12.col-xs-12.no-padding");
  73. q[2].parentNode.insertBefore(q[2], q[1]);
  74. //document.querySelector("div.padding15.col-sm-12.hidden-xs.mbl-display-n").scrollIntoView();
  75. if (description) {
  76. var input = document.getElementById("wmd-input");
  77. if (!input.value.endsWith(description))
  78. input.value += description;
  79. }
  80. document.getElementsByName("rafkodu")[0].scrollIntoView();
  81. }
  82. var pageState = restoreSavedObject("PageState", "pageState");
  83. var links = document.querySelectorAll("a.seller-link[href$='" + sellerId + ".html']");
  84. function findParent(el, tagName) {
  85. while ((el = el.parentElement) && el.tagName.toLowerCase() !== tagName);
  86. return el;
  87. }
  88. if (links.length == 1 && pageState.self) {
  89. var url = findParent(findParent(links[0], "div"), "div").querySelector("a").href;
  90. var id = url.substring(url.lastIndexOf("-kitap") + 6, url.length - 5);
  91. location.href = "https://www.desibona.com/zimba-makinesi" + id;
  92. return;
  93. }
  94. for (var index = 0; index < links.length; index++) {
  95. var url = findParent(findParent(links[index], "div"), "div").querySelector("a").href;
  96. var id = url.substring(url.lastIndexOf("-kitap") + 6, url.length - 5);
  97. var li = findParent(findParent(links[index], "li"), "li");
  98. var parent = li.parentElement;
  99. parent.removeChild(li);
  100. parent.prepend(li);
  101. var div = document.createElement("div");
  102. div.className = "col-md-12 no-padding text-right";
  103. div.innerHTML = '<br><br><a href="https://www.desibona.com/cift-kisilik-nevresim-takimi' + id + '" style="color:red;">Düzenle</a>';
  104. li.querySelector("div.col-md-4.col-xs-12.col-sm-12").appendChild(div);
  105. }
  106. var pendingRequestCount = 0;
  107. var successfulRequestCount = 0;
  108. window.addEventListener('beforeunload', function (e) {
  109. if (pendingRequestCount) {
  110. e.returnValue = 'Emin misin?';
  111. }
  112. });
  113. function restoreSavedObject(objectType, storageKey) {
  114. var obj = eval("new " + objectType + "();");
  115. var s = GM_getValue(storageKey);
  116. Object.assign(obj, s);
  117. return obj;
  118. }
  119. function IsbnInfo(isbnstr) {
  120. if (isbnstr && isbnstr.length == 13) {
  121. var t = 0, c;
  122. var digits = isbnstr.substr(3, 9);
  123. for (var i = 0; i < 9; i++) {
  124. t += (10 - i) * parseInt(digits[i]);
  125. }
  126. c = 11 - t % 11;
  127. this.isbn = digits + ((c = (11 - t % 11) % 11) == 10 ? 'X' : c);
  128. this.isbn13 = isbnstr;
  129. }
  130. else if (isbnstr && isbnstr.length == 10) {
  131. var sum = 38;
  132. for (var i = 0; i < 9; i++) {
  133. sum += parseInt(isbnstr[i]) * ((i % 2) ? 1 : 3);
  134. }
  135. var checkDigit = 10 - (sum % 10);
  136. if (checkDigit == 10) {
  137. checkDigit = 0;
  138. }
  139. this.isbn = isbnstr;
  140. this.isbn13 = "978" + isbnstr.substring(0, 9) + checkDigit;
  141. }
  142. else {
  143. this.isbn = this.isbn13 = this.title = "Unknown";
  144. }
  145. }
  146. IsbnInfo.prototype.save = function (title) {
  147. this.title = title;
  148. GM_setValue("isbnInfo", this);
  149. };
  150. var isbnInfo = restoreSavedObject("IsbnInfo", "isbnInfo");
  151. function AmazonBookInfo(/*isbn, title, author, publisher, pageSize, width, heigth*/) {
  152. // this.isbn = isbn;
  153. // this.title = title;
  154. // this.author = author;
  155. // this.publisher = publisher;
  156. // this.pageSize = pageSize;
  157. // this.width = width;
  158. // this.heigth = heigth;
  159. }
  160. AmazonBookInfo.prototype.save = function (storage) {
  161. GM_setValue(storage, this);
  162. };
  163. AmazonBookInfo.parseFromResponse = function (response, type) {
  164. var dom = new DOMParser().parseFromString(response.responseText, "text/html");
  165. var info = new AmazonBookInfo();
  166. info.title = dom.getElementById("productTitle").innerText.replace("(Turkish Edition)", "").trim();
  167. var q = dom.querySelector(".author.notFaded a.a-link-normal.contributorNameID")
  168. || dom.querySelector(".author.notFaded a.a-link-normal");
  169. info.author = q ? q.innerText.trim() : "";
  170. q = dom.getElementsByClassName("rpi-icon book_details-publisher");
  171. info.publisher = q.length ? q[0].parentNode.parentNode.lastElementChild.innerText.trim() : "";
  172. q = dom.getElementsByClassName("rpi-icon book_details-fiona_pages");
  173. info.pageCount = q.length ? q[0].parentNode.parentNode.lastElementChild.innerText.trim().split(" ")[0] : "";
  174. q = dom.getElementsByClassName("rpi-icon book_details-dimensions");
  175. if (q.length) {
  176. var dimensions = q[0].parentNode.parentNode.lastElementChild.innerText.trim().split(" x ");
  177. if (type == 1) {
  178. info.width = dimensions[0];
  179. info.heigth = dimensions[2];
  180. }
  181. else {
  182. info.width = (Math.floor((parseFloat(dimensions[0]) * 2.54) * 100) / 100).toString();
  183. info.heigth = (Math.floor((parseFloat(dimensions[2]) * 2.54) * 100) / 100).toString();
  184. }
  185. }
  186. else {
  187. info.width = "";
  188. info.heigth = "";
  189. }
  190. return info;
  191. };
  192. function PageState() {
  193. }
  194. PageState.prototype.save = function () {
  195. GM_setValue("pageState", this);
  196. };
  197. PageState.prototype.restore = function () {
  198. var _this = this;
  199. var inputs = document.querySelectorAll("input[data-stateful]");
  200. for (var index = 0; index < inputs.length; index++) {
  201. let input = inputs[index];
  202. let storage = input.getAttribute("data-stateful") || input.id.substring(3);
  203. input.addEventListener("change", function () {
  204. _this[storage] = this.type == "checkbox" ? this.checked : this.value;
  205. _this.save();
  206. });
  207. var value = this[storage];
  208. if (value) {
  209. if (typeof value == "boolean") {
  210. input.checked = value;
  211. }
  212. else {
  213. input.value = value;
  214. }
  215. }
  216. }
  217. };
  218. var div = document.createElement("div");
  219. div.innerHTML = `
  220. <div>
  221. <form action="kitapara.php" method="GET" name="searchbook" id="frmSearch" >
  222. <input type="hidden" name="ara" value="aramayap" />
  223. <input type="hidden" name="tip" value="kitap" />
  224. <input type="hidden" name="satici" value="` + sellerId + `" id="hdnSeller">
  225. <input type="hidden" name="siralama" value="fiyatartan" id="hdnSort">
  226. <span>
  227. <input id="chktitleChecked" type="checkbox" data-stateful />
  228. <input style="min-width: 400px;" id="txttitle" name="kitap_Adi" type="text" data-stateful />
  229. <input type="button" onclick='document.getElementById("kelime").value = document.getElementById("txttitle").value;document.getElementById("search-form").submit(); event.preventDefault(); return false;' value="Search" />
  230. </span>
  231. <span>
  232. <input id="chkauthorChecked" type="checkbox" data-stateful />
  233. <input id="txtauthor" name="yazar" type='text' data-stateful />
  234. </span>
  235. <span>
  236. <input id="chkpublisherChecked" type="checkbox" data-stateful />
  237. <input id="txtpublisher" name="yayin_Evi" type="text" data-stateful />
  238. </span>
  239. <span>
  240. <input id="chkself" type="checkbox" data-stateful />
  241. <label for="chkself">Kendimde</label>
  242. <input type="submit" value="ARA" />
  243. </span>
  244. </div>
  245. <div>
  246. <a href="https://www.desibona.com/baston-bebek-arabasi-puset>Baston Bebek Arabası</a>
  247. <a href="https://www.desibona.com/oyun-parki-besik-yatak">park yatak</a>
  248. <a href="https://www.desibona.com/mama-sandalyesi">Mama sandalyesi</a>
  249. <a href="https://www.desibona.com/cift-kisilik-nevresim-takimi"ift kişilik nevresim takımı</a>
  250. <a href="https://www.desibona.com/cift-kisilik-yatak-ortusu"ift kişilik yatak örtüsü</a>
  251. <a href=" https://www.desibona.com/kopek-mamasi">Köpek maması</a>
  252. <input type="button" value="` + isbnInfo.title + `" onclick='window.open("https://www.google.com.tr/search?q=kitap+` + isbnInfo.title + `", "google");' />
  253. <input id="chkpreferComtr" type="checkbox" data-stateful />
  254. <label for="chkpreferComtr">Prefer desibona.com</label>
  255. <input id="chkpasteIsbn" type="checkbox" data-stateful />
  256. <label for="chkpasteIsbn">PasteIsbn</label>
  257. </form>
  258. </div>`;
  259. document.title = isbnInfo.title;
  260. document.body.firstElementChild.appendChild(div);
  261. var isbnInput = document.querySelector('.form-control.all-input[name="isbn"]');
  262. if (isbnInput && pageState.pasteIsbn) {
  263. isbnInput.value = isbnInfo.isbn13;
  264. }
  265. var rafKodu = document.getElementsByName("rafkodu");
  266. if (rafKodu.length) {
  267. rafKodu[0].setAttribute("data-stateful", "shelfCode");
  268. }
  269. pageState.restore();
  270. document.getElementById("chkpreferComtr").addEventListener("change", function () {
  271. fillSearchForm();
  272. });
  273. function submitForm() {
  274. document.getElementById("txttitle").disabled = !document.getElementById("chktitleChecked").checked;
  275. document.getElementById("txtauthor").disabled = !document.getElementById("chkauthorChecked").checked;
  276. document.getElementById("txtpublisher").disabled = !document.getElementById("chkpublisherChecked").checked;
  277. document.getElementById("hdnSeller").disabled = !document.getElementById("chkself").checked;
  278. document.getElementById("frmSearch").submit();
  279. }
  280. var regexIsbn = /\d{13}/;
  281. var regexIsbn10 = /\d{10}/;
  282. document.getElementById("frmSearch").onsubmit = function (e) {
  283. e.preventDefault();
  284. var inputs = this.querySelectorAll("input[type=text]");
  285. for (var index = 0; index < inputs.length; index++) {
  286. var input = inputs[index];
  287. var str = input.value;
  288. var match;
  289. if (str && (match = str.match(regexIsbn))) {
  290. input.value = input.value.replace(match[0], "");
  291. input.dispatchEvent(new Event("change"));
  292. searchIsbn(match[0]);
  293. return false;
  294. }
  295. else if (str && (match = str.match(regexIsbn10))) {
  296. input.value = input.value.replace(match[0], "");
  297. input.dispatchEvent(new Event("change"));
  298. searchIsbn(match[0]);
  299. return false;
  300. }
  301. }
  302. submitForm();
  303. return false;
  304. };
  305. function fillSearchForm() {
  306. var main, other;
  307. var info0 = restoreSavedObject("AmazonBookInfo", "amazonInfo0");
  308. var info1 = restoreSavedObject("AmazonBookInfo", "amazonInfo1");
  309. if (document.getElementById("chkpreferComtr").checked) {
  310. main = info1;
  311. other = info0;
  312. }
  313. else {
  314. main = info0;
  315. other = info1;
  316. }
  317. var input = document.getElementById("txttitle");
  318. pageState.title = input.value = main.title || other.title || "";
  319. input = document.getElementById("txtauthor");
  320. pageState.author = input.value = main.author || other.author || "";
  321. input = document.getElementById("txtpublisher");
  322. pageState.publisher = input.value = main.publisher || other.publisher || "";
  323. pageState.save();
  324. }
  325. function searchIsbn(isbnstr) {
  326. var lisbnInfo = new IsbnInfo(isbnstr);
  327. GM_setValue("amazonInfo0", undefined);
  328. GM_setValue("amazonInfo1", undefined);
  329. pendingRequestCount = 2;
  330. successfulRequestCount = 0;
  331. document.title = "Loading";
  332. var onProgress = function (state, type, result) {
  333. switch (state) {
  334. case "info":
  335. document.title = result;
  336. break;
  337. case "error":
  338. alert("info: amazon" + (type == 0 ? ".com" : "com.tr") + " failed");
  339. if (!--pendingRequestCount) {
  340. if (!successfulRequestCount) {
  341. alert("Search yielded no results!");
  342. lisbnInfo.save();
  343. location.reload();
  344. }
  345. else {
  346. lisbnInfo.save(restoreSavedObject("AmazonBookInfo", "amazonInfo" + (type == 0 ? "1" : "0")).title || "No Title?");
  347. fillSearchForm();
  348. submitForm();
  349. }
  350. return;
  351. }
  352. break;
  353. case "complete":
  354. successfulRequestCount++;
  355. var info = AmazonBookInfo.parseFromResponse(result);
  356. info.save("amazonInfo" + type);
  357. if (!--pendingRequestCount) {
  358. lisbnInfo.save(info.title || restoreSavedObject("AmazonBookInfo", "amazonInfo" + (type == 0 ? "1" : "0")).title || "No Title?");
  359. fillSearchForm();
  360. submitForm();
  361. return;
  362. }
  363. break;
  364. }
  365. };
  366. fetchAmazon(lisbnInfo, 0, onProgress);
  367. fetchAmazon(lisbnInfo, 1, onProgress);
  368. }
  369. function fetchAmazon(isbInfo, type, onProgress) {
  370. var url;
  371. if (!type) {
  372. url = "https://amazon.com/dp/" + isbInfo.isbn;
  373. }
  374. else {
  375. url = "https://amazon.com.tr/dp/" + isbInfo.isbn;
  376. }
  377. GM.xmlHttpRequest({
  378. method: "GET",
  379. url: url,
  380. onload: function (response) {
  381. if (response.status !== 404) {
  382. onProgress("complete", type, response);
  383. return;
  384. } else {
  385. onProgress("info", type, "Searching...");
  386. if (type === 0) {
  387. url = "https://desibona.com/" + isbInfo.isbn;
  388. url = "https://desibona.com=" + isbInfo.isbn13;
  389. }
  390. else {
  391. url = "https://desibona.com" + isbInfo.isbn13;
  392. }
  393. GM.xmlHttpRequest({
  394. method: "GET",
  395. url: url,
  396. onload: function (response) {
  397. if (response.status === 404) {
  398. onProgress("error", type);
  399. return;
  400. }
  401. var dom = new DOMParser().parseFromString(response.responseText, "text/html");
  402. var q = dom.querySelectorAll("div.s-result-item a.a-link-normal");
  403. if (q.length < 3) {
  404. onProgress("error", type);
  405. return;
  406. }
  407. onProgress("info", type, "Loading2..");
  408. url = q[0].href;
  409. if (!type) {
  410. url = url.replace(location.origin, "https://www.amazon.com");
  411. }
  412. else {
  413. url = url.replace(location.origin, "https://www.amazon.com.tr");
  414. }
  415. GM.xmlHttpRequest({
  416. method: "GET",
  417. url: url,
  418. onload: function (response) {
  419. onProgress("complete", type, response);
  420. return;
  421. }
  422. });
  423. }
  424. });
  425. return;
  426. }
  427. }
  428. });
  429. }
  430. var timerId;
  431. var input = "";
  432. function restartTimeout() {
  433. if (timerId) {
  434. clearTimeout(timerId);
  435. }
  436. timerId = setTimeout(function () {
  437. input = "";
  438. }, 100);
  439. }
  440. document.addEventListener('keydown', function (e) {
  441. restartTimeout();
  442. if (e.key == "Enter" && input.length == 13) {
  443. searchIsbn(input);
  444. e.preventDefault();
  445. return false;
  446. }
  447. else if (e.key - '0' >= 0 && e.key - '0' <= 9) {
  448. input += e.key;
  449. }
  450. else {
  451. input = "";
  452. }
  453. return true;
  454. });
  455. })();