🏠 Home 

nyaablue

brings blue to nyaav2 with seadex api


Install this script?
  1. // ==UserScript==
  2. // @name nyaablue
  3. // @match https://nyaa.si/*
  4. // @grant GM.xmlHttpRequest
  5. // @grant GM.setValue
  6. // @grant GM.getValue
  7. // @version 0.4.0
  8. // @author garret
  9. // @description brings blue to nyaav2 with seadex api
  10. // @namespace garret's bad scripts
  11. // @connect sneedex.moe
  12. // ==/UserScript==
  13. function apply_blue(ids) {
  14. if (window.location.href.match("view")) {
  15. set_view_blue(ids)
  16. } else {
  17. set_search_blue(ids)
  18. }
  19. }
  20. function set_view_blue(ids) {
  21. let page_id = window
  22. .location
  23. .href
  24. .match("view/([0-9]+)")[1]
  25. if (ids.has(page_id)) {
  26. document.getElementsByClassName("panel-title")[0].parentNode.parentNode.className = "panel panel-info";
  27. }
  28. }
  29. function set_search_blue(ids) {
  30. let torrentlist = document.getElementsByClassName("torrent-list")[0].tBodies[0].children;
  31. for (const i of torrentlist) {
  32. let id = i
  33. .cells[1]
  34. .children[0]
  35. .attributes
  36. .href
  37. .value
  38. .match("view/([0-9]+)")[1]
  39. if (ids.has(id)) {
  40. i.className = "info"
  41. }
  42. }
  43. }
  44. function main(last_modified, cached_ids) {
  45. console.log("getting data")
  46. GM.xmlHttpRequest({
  47. headers: {
  48. "Accept": "application/json",
  49. "User-Agent": "nyaablue.user.js/" + GM_info.script.version,
  50. "If-Modified-Since": last_modified,
  51. },
  52. method: "GET",
  53. url: "https://sneedex.moe/api/public/nyaa",
  54. onload: function(response) {
  55. let ids = new Set(cached_ids);
  56. if (response.status != "304") {
  57. console.log("has been modified since last check");
  58. let entries = JSON.parse(response.responseText);
  59. ids = new Array()
  60. for (const i of entries) {
  61. for (const j of i.nyaaIDs) {
  62. ids.push(String(j))
  63. }
  64. }
  65. GM.setValue("ids", ids);
  66. ids = new Set(ids)
  67. } else {
  68. console.log("304, it's not changed since last time");
  69. }
  70. if (response.responseHeaders.search(/last\-modified:/i) !== -1){
  71. let last_modified = response.responseHeaders.match(/last\-modified: *(.+)/i)[1];
  72. GM.setValue("last_modified", last_modified);
  73. }
  74. GM.setValue("last_update", Date.now());
  75. apply_blue(ids);
  76. }
  77. })
  78. }
  79. async function check_cache() {
  80. let cached_ids = new Set();
  81. let last_update = 0;
  82. let lm = "";
  83. try {
  84. cached_ids = await GM.getValue("ids");
  85. last_update = await GM.getValue("last_update");
  86. lm = await GM.getValue("last_modified");
  87. if (typeof last_update !== "number") {
  88. throw "probably first run"
  89. }
  90. if (Date.now() > last_update + 3600000) { // not updated in 1 hour
  91. console.log("cache expired")
  92. throw "cache expired";
  93. } else {
  94. console.log("cache isnt expired");
  95. if (cached_ids == undefined) {
  96. console.log("but we dont have any data...");
  97. lm = "";
  98. throw "no data";
  99. }
  100. if (typeof cached_ids !== "object" || cached_ids[1] == undefined) {
  101. console.log("but data is bad");
  102. lm = "";
  103. throw "bad data"
  104. }
  105. console.log("and the data seems fine™");
  106. apply_blue(new Set(cached_ids))
  107. }
  108. } catch {
  109. console.log("ooer, handling exception")
  110. main(lm, cached_ids)
  111. }
  112. }
  113. // dark theme "support" (thanks olli)
  114. document.head.insertAdjacentHTML('beforeend', '<style id="css_blue" type="text/css">body.dark .torrent-list > tbody > tr.info > td {color: inherit; background-color: rgba(0, 172, 255, 0.12);} body.dark .torrent-list > tbody > tr.info:hover > td {background-color: rgba(0, 172, 255, 0.18);} body.dark div.panel-info, body.dark div.panel-info > .panel-heading {border-color: #2c414b;} body.dark div.panel-info > .panel-heading {background-color: #2a3f4a;}</style>');
  115. check_cache()