Collapse/expand all HIT details - TurkerView

Collapses/expands all HIT details

  1. // ==UserScript==
  2. // @name Collapse/expand all HIT details - TurkerView
  3. // @version 0.1
  4. // @description Collapses/expands all HIT details
  5. // @author lucassilvas1
  6. // @match https://turkerview.com/requesters/*
  7. // @grant GM_addStyle
  8. // jshint esversion: 6
  9. // @namespace https://greasyfork.org/users/846945
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. "use strict";
  14.  
  15. document.getElementById("liHITs").addEventListener(
  16. "click",
  17. () => {
  18. let collapsed = true;
  19.  
  20. const checkIfExists = setInterval(() => {
  21. const hitsTab = document.getElementById("HITs");
  22. if (hitsTab) {
  23. clearInterval(checkIfExists);
  24.  
  25. const rows = document
  26. .getElementsByTagName("tbody")[0]
  27. .querySelectorAll("tr");
  28. const detailRows = [...rows].filter((_, i) => i % 2 != 0);
  29.  
  30. GM_addStyle(`
  31. #collapse-expand-btn {
  32. display: inline-block;
  33. float: right;
  34. background-color: transparent;
  35. border: none;
  36. text-decoration: underline;
  37. }
  38. `);
  39.  
  40. hitsTab.insertAdjacentHTML(
  41. "afterbegin",
  42. '<button type="button" id="collapse-expand-btn">expand all</button>'
  43. );
  44.  
  45. const button = document.getElementById("collapse-expand-btn");
  46.  
  47. button.addEventListener("click", () => {
  48. if (collapsed) {
  49. button.textContent = "collapse all";
  50. collapsed = false;
  51. detailRows.forEach((row) => row.classList.remove("hidden"));
  52. } else {
  53. button.textContent = "expand all";
  54. collapsed = true;
  55. detailRows.forEach((row) => row.classList.add("hidden"));
  56. }
  57. });
  58. }
  59. }, 300);
  60. },
  61. { once: true }
  62. );
  63. })();