Refresh/Clear NBViewer Cache

Add a button to navigation bar to refresh/clear the cache of nbviewer

  1. // ==UserScript==
  2. // @name Refresh/Clear NBViewer Cache
  3. // @namespace https://peijunz.github.io
  4. // @author Peijun Zhu
  5. // @description Add a button to navigation bar to refresh/clear the cache of nbviewer
  6. // @include *://nbviewer.jupyter.org/*.ipynb*
  7. // @version 2.4.1
  8. // @grant none
  9. // @icon https://github.com/jupyter/design/raw/master/logos/Square%20Logo/squarelogo-greytext-orangebody-greymoons/squarelogo-greytext-orangebody-greymoons.png
  10. // ==/UserScript==
  11. function flush() {
  12. var url = document.location.href;
  13. while (url.substr( - 1) === '/' || url.substr( - 1) === '#') {
  14. //Strip # or / in the end
  15. url = url.substr(0, url.length - 1);
  16. }
  17. var tail = '?flush_cache=true';
  18. if (!url.endsWith(tail)) {
  19. document.location.href = url + tail;
  20. }
  21. else {
  22. document.location.reload();
  23. }
  24. }
  25. function add_flush_button() {
  26. var lu = document.getElementsByClassName('nav navbar-nav navbar-right') [0];
  27. var li = document.createElement('li');
  28. var a = document.createElement('a');
  29. li.appendChild(a);
  30. lu.appendChild(li);
  31. a.href = '#';
  32. a.onclick = flush;
  33. a.innerHTML = '⟳';
  34. a.style['font-size']="2.5em";
  35. }
  36. add_flush_button();