Advertisement
Guest User

Untitled

a guest
May 29th, 2024
5,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Amazon Search LibGen
  3. // @version 1.0.0
  4. // @description It add amazon's books page a link in order to search on libGen Library Genesis the title, author o ISBN code of the viewed books
  5. // @author Flejta
  6. // @match https://www.amazon.com/*
  7. // @match https://www.amazon.fr/*
  8. // @match https://www.amazon.de/*
  9. // @match https://www.amazon.co.uk/*
  10. // @match https://www.amazon.it/*
  11. // @match https://www.amazon.%2A/*
  12. // @grant none
  13. // @namespace https://greasyfork.org/users/859328
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. var BASEURL = "http://gen.lib.rus.ec/";
  19.  
  20. function getAuthor() {
  21. var author = document.querySelector('span.author span a.contributorNameID')
  22. || document.querySelector('span.author a.a-link-normal')
  23. || document.getElementById('bylineContributor');
  24. return author.innerText.trim();
  25. }
  26.  
  27. function getTitleElem() {
  28. return document.getElementById('productTitle')
  29. || document.getElementById('title')
  30. || document.getElementById('ebooksTitle');
  31. }
  32.  
  33. function getTitle() {
  34. var title = getTitleElem();
  35. return title.innerText.match(new RegExp("([^(\[)]+)"))[0].trim();
  36. }
  37.  
  38. function addLinkElem(id, elem) {
  39. document.getElementById(id).appendChild(elem);
  40. }
  41.  
  42. function createLibgenDiv() {
  43. // outer div
  44. var outerDiv = document.createElement('div');
  45. outerDiv.className='a-size-base';
  46.  
  47.  
  48. if (document.getElementById('dp') && document.getElementById('dp').className.trim() == 'ebooks_mobile') {
  49. outerDiv.style.margin = '20px';
  50. document.getElementById('dp').insertBefore(outerDiv, document.getElementsByClassName('a-container')[0]);
  51. } else {
  52. outerDiv.style.margin="5px";
  53. var title = getTitleElem();
  54. title.parentNode.appendChild(outerDiv);
  55. }
  56. outerDiv.style.padding="5px";
  57. outerDiv.style.border = "2px solid rgb(128, 0, 0)";
  58. outerDiv.style.float = "right";
  59.  
  60. // libgen div
  61. var libgenDiv = document.createElement('div');
  62. libgenDiv.id = 'libgen';
  63. var b = document.createElement('b'); b.innerText = 'LibGen (Sci-Tech): '
  64. b.style.float = "left";
  65. libgenDiv.appendChild(b);
  66. outerDiv.appendChild(libgenDiv);
  67.  
  68. // fiction div
  69. var fictionDiv = document.createElement('div');
  70. fictionDiv.style.marginTop="2em";
  71. fictionDiv.id = 'fiction';
  72. var b2 = document.createElement('b'); b2.innerText = 'LibGen Fiction: '
  73. b2.style.float = "left";
  74. fictionDiv.appendChild(b2);
  75. outerDiv.appendChild(fictionDiv);
  76. }
  77.  
  78.  
  79. function createLink(href, text) {
  80. var container = document.createElement('div');
  81. container.style.float = "left";
  82. container.style.paddingLeft = "10px";
  83. var innerContainer = document.createElement('div');
  84. innerContainer.style.border = "1px solid rgb(127, 127, 127)";
  85. innerContainer.style.paddingLeft = "5px";
  86. innerContainer.style.paddingRight = "5px";
  87. var a = document.createElement('a');
  88. a.href = href;
  89. a.innerText = text;
  90. innerContainer.appendChild(a);
  91. container.appendChild(innerContainer);
  92. return container;
  93. }
  94.  
  95.  
  96. function addIsbnSearchLink(isbnType) {
  97. Array.from(document.querySelectorAll('span.a-text-bold'))
  98. .filter(elem => elem.innerText.indexOf(isbnType) != -1)
  99. .map(isbnTextElement => {
  100. var isbn = isbnTextElement.nextSibling.nextSibling.innerText.trim();
  101. var url = BASEURL + "search.php?req=" + isbn+ "&lg_topic=libgen&open=0&view=simple&res=25&phrase=1&column=identifier";
  102. addLinkElem('libgen', createLink(url, isbnType.toLowerCase()));
  103. });
  104.  
  105. // ugly copy&paste for mobile support
  106. Array.from(document.getElementsByTagName('th'))
  107. .filter(elem => elem.innerText.indexOf(isbnType) != -1)
  108. .map(isbnTextElement => {
  109. var isbn = isbnTextElement.nextElementSibling.textContent.trim();
  110. var url = BASEURL + "search.php?req=" + isbn+ "&lg_topic=libgen&open=0&view=simple&res=25&phrase=1&column=identifier";
  111. addLinkElem('libgen', createLink(url, isbnType.toLowerCase()));
  112. });
  113. }
  114.  
  115. function addLibgenSearchLink(searchString, linkText) {
  116. var url = BASEURL + "search.php?req=" + encodeURIComponent(searchString)
  117. + "&open=0&res=25&view=simple&phrase=1&column=def";
  118. addLinkElem('libgen', createLink(url, linkText))
  119. }
  120.  
  121. function addFictionSearchLink(searchString, linkText) {
  122. var url = BASEURL + "fiction/?q=" + encodeURIComponent(searchString);
  123. addLinkElem('fiction', createLink(url, linkText))
  124. }
  125.  
  126. createLibgenDiv();
  127.  
  128. var author = getAuthor();
  129. var title = getTitle();
  130.  
  131. if (author && title) {
  132. var searchString = author + ' ' + title;
  133. addLibgenSearchLink(searchString, "Author + Title");
  134. addFictionSearchLink(searchString, "Author + Title");
  135. }
  136.  
  137. if (title) {
  138. addLibgenSearchLink(title, "Title");
  139. addFictionSearchLink(title, "Title");
  140. }
  141.  
  142. if (author) {
  143. addLibgenSearchLink(author, "Author");
  144. addFictionSearchLink(author, "Author");
  145. }
  146.  
  147. addIsbnSearchLink('ISBN-10');
  148. addIsbnSearchLink('ISBN-13');
  149. })();
  150.  
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement