Generate Table of Contents for ar5iv

Generates a table of contents for ar5iv papers, displayed on the left side of the page.

  1. // ==UserScript==
  2. // @name Generate Table of Contents for ar5iv
  3. // @namespace awyugan
  4. // @description Generates a table of contents for ar5iv papers, displayed on the left side of the page.
  5. // @match https://ar5iv.labs.arxiv.org/*
  6. // @license MIT
  7. // @version 1.0
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. // Create a div for the table of contents
  15. var tocDiv = document.createElement('div');
  16. tocDiv.style.position = 'fixed';
  17. tocDiv.style.left = '0';
  18. tocDiv.style.top = '0';
  19. tocDiv.style.border = '1px solid black';
  20. tocDiv.style.padding = '10px';
  21. tocDiv.style.backgroundColor = '#f9f9f9';
  22. tocDiv.style.width = '200px';
  23. tocDiv.style.height = '100vh';
  24. tocDiv.style.overflow = 'auto';
  25.  
  26. // Create a title for the table of contents
  27. var tocTitle = document.createElement('h2');
  28. tocTitle.textContent = 'Table of Contents';
  29. tocDiv.appendChild(tocTitle);
  30.  
  31. // Get all h1, h2, h3, h4, h5 and h6 elements
  32. var headings = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
  33.  
  34. // Create a link for each heading and add it to the table of contents
  35. headings.forEach(function(heading, index) {
  36. var link = document.createElement('a');
  37. link.textContent = heading.textContent;
  38. link.href = '#heading' + index;
  39.  
  40. // Add an id to the heading so the link can refer to it
  41. heading.id = 'heading' + index;
  42.  
  43. // Adjust link style based on heading level
  44. var headingLevel = parseInt(heading.tagName[1]);
  45. link.style.display = 'block';
  46. link.style.textIndent = (headingLevel - 1) * 10 + 'px';
  47. link.style.fontSize = (7 - headingLevel) + 'pt';
  48.  
  49. tocDiv.appendChild(link);
  50. });
  51.  
  52. // Add the table of contents to the body
  53. document.body.appendChild(tocDiv);
  54. })();