🏠 Home 

PTT 404 Redirect to PTTWeb

PTT 網頁 404 的時候 導頁到 PTTWeb


Install this script?
  1. // ==UserScript==
  2. // @name PTT 404 Redirect to PTTWeb
  3. // @namespace https://github.com/livinginpurple
  4. // @version 2023.01.31.01
  5. // @description PTT 網頁 404 的時候 導頁到 PTTWeb
  6. // @license WTFPL
  7. // @author livinginpurple
  8. // @match https://*.ptt.cc/*
  9. // @run-at document-start
  10. // @grant none
  11. // @grant GM.xmlHttpRequest
  12. // ==/UserScript==
  13. (function() {
  14. 'use strict';
  15. const scriptName = GM_info.script.name;
  16. console.log(`${scriptName} is loading.`);
  17. function urlExists(url, callback) {
  18. fetch(url, { method: 'head' })
  19. .then(function (status) {
  20. callback(status.ok)
  21. });
  22. }
  23. let url = document.location.href;
  24. urlExists(url, function (exists) {
  25. if (exists) {
  26. // it exists, do something
  27. } else {
  28. location.href = url.replace('ptt', 'pttweb');
  29. }
  30. });
  31. console.log(`${scriptName} is running.`);
  32. })();