LibraryThing links to WorldCat on combination/separation pages

Adds direct links to WorldCat searches for the editions shown in the combine/separate pages

  1. // ==UserScript==
  2. // @name LibraryThing links to WorldCat on combination/separation pages
  3. // @namespace https://greasyfork.org/en/users/11592-max-starkenburg
  4. // @description Adds direct links to WorldCat searches for the editions shown in the combine/separate pages
  5. // @include http*://*librarything.tld/combine.php?*
  6. // @include http*://*librarything.com/combine.php?*
  7. // @version 2
  8. // ==/UserScript==
  9.  
  10. var paras = document.getElementsByTagName("table")[0].getElementsByTagName("p");
  11. var wcOnclick = "onclick='event.stopPropagation()'"; // To prevent the new link from "selecting" that work for combination purposes
  12.  
  13. for ( var i=0; i<paras.length; i++ ) {
  14.  
  15. var para = paras[i];
  16. var link = "";
  17. var wcSpan = document.createElement("span");
  18. var editionText = para.innerHTML; // "[Media -] Title / [Author] / [(ISBN #)] / [Format] (separate)"
  19. // Remove any media format, such as "Video Recording"
  20. editionText = editionText.replace(/^<span class="formatnotice">[^<]+<\/span> \u2014 /, "");
  21.  
  22. // If there's an ISBN, use that
  23. var isbnRE = / \/[ ]+\(ISBN ([\dX]+)\)/;
  24. if (isbnRE.test(editionText)) {
  25. isbn = editionText.match(isbnRE)[1];
  26. link = '&nbsp;(<a ' + wcOnclick + ' href="http://worldcat.org/isbn/' + isbn + '">WorldCat</a>)'
  27.  
  28. // If there's no match for an ISBN, use title and possible author instead
  29. } else {
  30. // Remove the last part, including the separation link and any text format
  31. editionText = editionText.replace(/&nbsp;\(<.*$/, "") // (separate) link
  32. .replace(/ \/ <a .*$/, "") // Empty media format <a>
  33. var author = title = "";
  34. // Let's hope that if the title contains any forward slashes, there's an author following the last (non-titular) slash
  35. var titleAuthor = editionText.split(" / ");
  36. if (titleAuthor.length > 1) {
  37. author = titleAuthor[titleAuthor.length - 1];
  38. }
  39. authorSafe = author.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); // This replace from CoolAJ86 at http://stackoverflow.com/questions/3446170
  40. var authorRE = new RegExp(" / " + authorSafe + "$");
  41. title = editionText.replace(authorRE, "");
  42. query = "ti:" + title;
  43. if (author != "") {
  44. query = query + " au:" + author;
  45. }
  46. query = encodeURIComponent(query);
  47. link = '&nbsp;(<a ' + wcOnclick + ' href="http://worldcat.org/search?q=' + query + '">WorldCat</a>)';
  48. }
  49. // Add the newly constructed link to the end of the line
  50. wcSpan.innerHTML = link;
  51. para.appendChild(wcSpan);
  52.  
  53. }