Greasy Fork 支持简体中文。

Ordered-Lists-for-WorkFlowy

Enable ordered lists for WorkFlowy. Can also hide child bullets.

目前為 2022-07-01 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Ordered-Lists-for-WorkFlowy
  3. // @namespace https://github.com/BettyJJ
  4. // @version 0.4.0
  5. // @description Enable ordered lists for WorkFlowy. Can also hide child bullets.
  6. // @author Betty
  7. // @match https://workflowy.com/*
  8. // @match https://*.workflowy.com/*
  9. // @run-at document-idle
  10. // @grant GM.addStyle
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. 'use strict';
  15.  
  16.  
  17. init();
  18.  
  19.  
  20. /**
  21. * initialize
  22. */
  23. function init() {
  24. watch_page();
  25. add_style();
  26.  
  27. // we hide the bullets in ordered lists by default. If you want to show them by default, uncomment the next line
  28. // show_bullet_in_ol();
  29.  
  30. // if a node contains nothing but . (a period), we hide this bullet. If you do not want this behaviour, comment the next line
  31. add_style_nb_dot();
  32. }
  33.  
  34.  
  35. /**
  36. * watch the page
  37. */
  38. function watch_page() {
  39.  
  40. // wathe the page, so that the rendering is updated when new contents come in as the user edits or navigates
  41. const observer = new MutationObserver(function (mutationlist) {
  42. for (const { addedNodes } of mutationlist) {
  43. for (const node of addedNodes) {
  44. if (!node.tagName) continue; // not an element
  45.  
  46. if (node.classList.contains('innerContentContainer')) {
  47. add_class(node);
  48. }
  49.  
  50. }
  51. }
  52. });
  53. observer.observe(document.body, { childList: true, subtree: true });
  54.  
  55. }
  56.  
  57.  
  58. /**
  59. * if a node contains the #ol or #nb tag, add a class
  60. * * @param {Node} node Dom Node
  61. */
  62. function add_class(node) {
  63. // sometimes there is a dummy node without parent. don't know why, but we need to check and exclude it first
  64. if (!node.parentElement) {
  65. return;
  66. }
  67.  
  68. add_class_ol(node);
  69. add_class_nb(node);
  70. add_class_nb_dot(node);
  71. }
  72.  
  73.  
  74. /**
  75. * if a node contains the #ol tag, add a class
  76. * * @param {Node} node Dom Node
  77. */
  78. function add_class_ol(node) {
  79. // first remove the class we added previously
  80. const ancestor = node.closest('.project');
  81. ancestor.classList.remove('list-ol');
  82.  
  83. // if a node contains the #ol tag, add a class
  84. if (node.querySelectorAll('[data-val="#ol"]').length > 0) {
  85. ancestor.classList.add('list-ol');
  86. }
  87. }
  88.  
  89.  
  90. /**
  91. * if a node contains the #nb tag, add a class
  92. * * @param {Node} node Dom Node
  93. */
  94. function add_class_nb(node) {
  95. // first remove the class we added previously
  96. const ancestor = node.closest('.project');
  97. ancestor.classList.remove('list-nb');
  98.  
  99. // if a node contains the #nb tag, add a class
  100. if (node.querySelectorAll('[data-val="#nb"]').length > 0) {
  101. ancestor.classList.add('list-nb');
  102. }
  103. }
  104.  
  105.  
  106. /**
  107. * if a node contains nothing but . (a period), add a class
  108. * * @param {Node} node Dom Node
  109. */
  110. function add_class_nb_dot(node) {
  111. // first remove the class we added previously
  112. const ancestor = node.closest('.project');
  113. ancestor.classList.remove('list-nb-dot');
  114.  
  115. // if a node contains nothing but . (a period), add a class
  116. if (node.textContent === '.') {
  117. ancestor.classList.add('list-nb-dot');
  118. }
  119. }
  120.  
  121.  
  122. /**
  123. * add style
  124. */
  125. function add_style() {
  126. add_style_ol();
  127. add_style_nb();
  128.  
  129. // hide the tags (#ol, #nb) when the node are not in focus or hovered
  130. const css = `
  131. .contentTag[data-val="#ol"]:not(.name--focused *):not(.name:hover *),
  132. .contentTag[data-val="#nb"]:not(.name--focused *):not(.name:hover *) {
  133. opacity: 0;
  134. }
  135. `
  136.  
  137. GM.addStyle(css);
  138.  
  139. }
  140.  
  141.  
  142. /**
  143. * add style for #ol
  144. */
  145. function add_style_ol() {
  146. // hide the bullets
  147. let css = `
  148. .list-ol > .children > .project > .name > .bullet {
  149. opacity: 0;
  150. }
  151. .list-ol > .children > .project > .name > .bullet:hover {
  152. opacity: 0.5;
  153. }
  154. .list-ol > .children > .project > .name > .bullet > svg {
  155. fill: transparent;
  156. }
  157. `
  158.  
  159. // show the numbers
  160. css += `
  161. .list-ol > .children {
  162. counter-reset: counter;
  163. }
  164. .list-ol > .children > .project > .name:before {
  165. counter-increment: counter;
  166. content: counter(counter) '. ';
  167. float: left;
  168. margin-left: -18px;
  169. margin-top: 8px;
  170. }
  171. `
  172.  
  173. GM.addStyle(css);
  174. }
  175.  
  176.  
  177. /**
  178. * add style for #nb
  179. */
  180. function add_style_nb() {
  181. // hide the bullets
  182. const css = `
  183. .list-nb > .children > .project > .name > a.bullet {
  184. opacity: 0;
  185. }
  186. .list-nb > .children > .project > .name > a.bullet:hover {
  187. opacity: 0.5;
  188. }
  189. `
  190.  
  191. GM.addStyle(css);
  192. }
  193.  
  194.  
  195. /**
  196. * add style if a node contains nothing but . (a period)
  197. */
  198. function add_style_nb_dot() {
  199. // hide the bullets
  200. const css = `
  201. .list-nb-dot > .name > a.bullet {
  202. opacity: 0;
  203. }
  204. .list-nb-dot > .name > a.bullet:hover {
  205. opacity: 0.5;
  206. }
  207. .list-nb-dot > .name > .content:not(.name--focused *):not(.name:hover *) {
  208. opacity: 0;
  209. }
  210. `
  211.  
  212. GM.addStyle(css);
  213. }
  214.  
  215.  
  216. /**
  217. * unhide the bullets in ordered lists
  218. */
  219. function show_bullet_in_ol() {
  220. // unhide the bullets
  221. let css = `
  222. .list-ol > .children > .project > .name > .bullet {
  223. opacity: revert;
  224. }
  225. .list-ol > .children > .project > .name > .bullet:hover {
  226. opacity: revert;
  227. }
  228. .list-ol > .children > .project > .name > .bullet > svg {
  229. fill: revert;
  230. }
  231. `
  232.  
  233. // position the numbers
  234. css += `
  235. .list-ol > .children > .project > .name:before {
  236. margin-left: -3px;
  237. margin-right: 5px;
  238. }
  239. `
  240.  
  241. GM.addStyle(css);
  242. }
  243.  
  244.  
  245. })();