Capytale Jupyter Notebook : more tools

New features for Capytale's Jupyter's notebooks : (1) admonition question quick add, (2) restart & clear cells outputs shortcut

当前为 2025-01-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Capytale Jupyter Notebook : more tools
  3. // @namespace http://tampermonkey.net/
  4. // @version 2025-01-16
  5. // @description New features for Capytale's Jupyter's notebooks : (1) admonition question quick add, (2) restart & clear cells outputs shortcut
  6. // @author James Web (in the area)
  7. // @supportURL https://codeberg.org/jrm-omg/userscripts
  8. // @include https://capytale2.ac-paris.fr/p/basthon/**
  9. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16. // Wait until Basthon's ready
  17. let ticTac;
  18. function myCallback() {
  19. if (document.querySelector('#MathJax_Message')) {
  20. // Basthon is ready, go go go !
  21. clearInterval(ticTac);
  22.  
  23. // (1) Admonition question button feature
  24. function addAdmoQuestion() {
  25. let cell = Jupyter.notebook.insert_cell_below('markdown')
  26. cell.set_text('!!! question\n\n!!!')
  27. }
  28. let qEl = document.createElement('button');
  29. qEl.className = 'btn btn-default';
  30. qEl.title = 'Ajouter une admonition « question » après la cellule active';
  31. qEl.innerHTML = '<i class="fa-question-circle fa"></i>';
  32. qEl.addEventListener('click', addAdmoQuestion);
  33.  
  34. // (2) Restart & clear cells outputs shortcut
  35. function restartAndClean() {
  36. document.querySelector('#restart_run_all').click();
  37. setTimeout(()=>{
  38. document.querySelector('.modal-footer .btn-danger').click();
  39. setTimeout(()=>{
  40. document.querySelector('#restart_clear_output').click();
  41. setTimeout(()=>{
  42. document.querySelector('.modal-footer .btn-danger').click();
  43. }, 500)
  44. }, 500)
  45. }, 500)
  46. }
  47. let rEl = document.createElement('button');
  48. rEl.className = 'btn btn-default';
  49. rEl.title = 'Tout exécuter + Effacer les sorties';
  50. rEl.innerHTML = '<i class="fa-truck fa"></i>';
  51. rEl.addEventListener('click', restartAndClean);
  52.  
  53. // Insert new buttons inside the user-scripts-group
  54. let usgEl = document.querySelector('.user-scripts-group');
  55. if (!usgEl) {
  56. usgEl = document.createElement('div');
  57. usgEl.className = 'btn-group user-scripts-group';
  58. document.querySelector('#maintoolbar-container').append(usgEl);
  59. }
  60. usgEl.append(qEl);
  61. usgEl.append(rEl);
  62. }
  63. }
  64. ticTac = setInterval(myCallback, 500);
  65. })();