ao3 hide bookmarks

permanently hide bookmarks created by specified users

  1. // ==UserScript==
  2. // @name ao3 hide bookmarks
  3. // @namespace https://greasyfork.org/en/users/800073-bellisk
  4. // @version 0.1.2
  5. // @description permanently hide bookmarks created by specified users
  6. // @author bellisk
  7. // @include http://archiveofourown.org/works/*/bookmarks*
  8. // @include https://archiveofourown.org/works/*/bookmarks*
  9. // @grant GM.setValue
  10. // @grant GM.getValue
  11. // @grant GM.listValues
  12. // @grant GM.deleteValue
  13. // ==/UserScript==
  14.  
  15. const bookmarks = document.querySelectorAll('li.short');
  16.  
  17. // interface
  18.  
  19. let headerModule, blockLink, blockStyle;
  20. for (let i=0;i<bookmarks.length;i++) {
  21. headerModule = bookmarks[i].getElementsByClassName('header module')[0];
  22. blockLink = document.createElement('div');
  23. blockLink.className = 'bookmarkblock';
  24. blockLink.innerHTML = '<a class="blockThisBookmarker">block this bookmarker</a>';
  25. headerModule.parentNode.insertBefore(blockLink, headerModule.nextSibling);
  26. }
  27. blockStyle = document.createElement('style');
  28. blockStyle.innerHTML = 'div.bookmarkblock {text-align: right; font-family:monospace; margin-bottom: .375em; cursor: pointer;}';
  29. document.head.appendChild(blockStyle);
  30.  
  31. let unblock = document.createElement('li');
  32. unblock.innerHTML = `
  33. <a class="dropdown-toggle" href="/menu/hide-bookmarks" data-toggle="dropdown" data-target="#">Hide Bookmarks</a>
  34. <ul class="menu dropdown-menu" role="menu">
  35. <li id="clearLast" role="menu-item"><a href="#">Unblock last</a></li>
  36. <li id="clearAll" role="menu-item"><a href="#">Unblock all</a></li>
  37. <li id="blockUsername" role="menu-item"><a href="#">Block username</a></li>
  38. </ul>`;
  39. unblock.className = 'dropdown bookmarkblock';
  40. let search = document.getElementsByClassName('primary navigation actions')[0].getElementsByClassName('search')[0];
  41. search.parentNode.insertBefore(unblock, search);
  42.  
  43. // block bookmarks
  44.  
  45. function getBookmarkerName(liTag) {
  46. const byline = liTag.getElementsByClassName('byline')[0];
  47. const bookmarker = byline.getElementsByTagName('a')[0];
  48. return bookmarker.text;
  49. }
  50.  
  51. function blockThisBookmarker(bookmark) {
  52. const bookmarker = getBookmarkerName(bookmark);
  53. GM.setValue(bookmarker, bookmarker);
  54. GM.setValue('last', bookmarker);
  55. }
  56.  
  57. async function blockSelected(bookmarks) {
  58. const blocked = await GM.listValues();
  59. for (let j=0; j<bookmarks.length; j++) {
  60. const bookmarker = getBookmarkerName(bookmarks[j]);
  61. if (blocked.find(function(id){return id === bookmarker;})) {
  62. bookmarks[j].style.display = 'none';
  63. }
  64. }
  65. }
  66.  
  67. function blockUsername() {
  68. const username = prompt("Enter a username to hide all bookmarks from");
  69. GM.setValue(username, username);
  70. GM.setValue('last', username);
  71. location.reload();
  72. }
  73.  
  74. // unblock bookmarks
  75.  
  76. async function clearAll(){
  77. const keys = await GM.listValues();
  78. for (let k=0;k<keys.length; k++) {
  79. await GM.deleteValue(keys[k]);
  80. }
  81. location.reload();
  82. }
  83.  
  84. async function clearLast() {
  85. const username = await GM.getValue('last');
  86. await GM.deleteValue('last');
  87. await GM.deleteValue(username);
  88. const blocked = await GM.listValues();
  89. GM.setValue('last', blocked[blocked.length -1]);
  90. location.reload();
  91. }
  92.  
  93. // run
  94.  
  95. blockSelected(bookmarks);
  96.  
  97. document.getElementById('clearLast').onclick = function() {clearLast();};
  98. document.getElementById('clearAll').onclick = function() {clearAll();};
  99. document.getElementById('blockUsername').onclick = function() {blockUsername();};
  100.  
  101. const blockLinks = document.getElementsByClassName('blockThisBookmarker');
  102. for (let k=0; k<blockLinks.length; k++) {
  103. let bLink = blockLinks[k];
  104. bLink.onclick = function() {
  105. let bookmark = this.parentNode.parentNode;
  106. blockThisBookmarker(bookmark)
  107. bookmark.style.display = "none";
  108. };
  109. }