Always Active Codespaces

Automatically restarts inactive Codespaces on GitHub Dev when timed out.

  1. // ==UserScript==
  2. // @name Always Active Codespaces
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description Automatically restarts inactive Codespaces on GitHub Dev when timed out.
  6. // @author SC0TT
  7. // @license MIT
  8. // @icon https://github.gallerycdn.vsassets.io/extensions/github/codespaces/1.16.10/1707254009255/Microsoft.VisualStudio.Services.Icons.Default
  9. // @match https://*.github.dev/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. function restartCodespace() {
  17. const restartButton = document.querySelector('button.button-link');
  18. if (restartButton) {
  19. restartButton.click();
  20. } else {
  21. console.error("Error: Unable to find the 'Restart codespace' button on the page.");
  22. }
  23. }
  24.  
  25. function handleMutations(mutations) {
  26. mutations.forEach(mutation => {
  27. if (mutation.type === 'childList') {
  28. restartCodespace();
  29. }
  30. });
  31. }
  32.  
  33. const observer = new MutationObserver(handleMutations);
  34.  
  35. observer.observe(document.body, { childList: true, subtree: true });
  36.  
  37. window.addEventListener('load', function() {
  38. restartCodespace();
  39. });
  40. })();