Redirect es to ca

Tries to redirect any url with /es or es. to /ca ca.

当前为 2023-06-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Redirect es to ca
  3. // @description Tries to redirect any url with /es or es. to /ca ca.
  4. // @namespace https://github.com/tonioriol
  5. // @version 0.0.3
  6. // @match *://*/*
  7. // @grant GM.getValue
  8. // @grant GM.setValue
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12.  
  13. const popover = `
  14. <div style="position: absolute; top: 50px; left: 50px; z-index: 999999; padding: 10px 20px; color: white; background-color: black; font-size: 32px; display: flex; align-items: center; gap: 16px" onclick="this.hidden=true">
  15. Redirigit a la versió en català. <span style="font-size: 1.5em">✖</span>
  16. </div>
  17. `
  18.  
  19.  
  20. const notify = () => {
  21. const div = document.createElement('div')
  22. div.innerHTML = popover
  23. document.body.appendChild(div)
  24. setTimeout(() => {
  25. div.hidden = true
  26. }, 3000)
  27. }
  28.  
  29. (async () => {
  30. window.addEventListener('load', async () => {
  31. const notf = await GM.getValue('notify', false)
  32.  
  33. if (notf) {
  34. await GM.setValue('notify', false)
  35. notify()
  36. }
  37. })
  38.  
  39. const oldHref = location.href
  40. const fixedHref = location.href.replace(/(\/es\/)/, '/ca/')
  41.  
  42. if (location.href !== fixedHref) {
  43. const response = await fetch(fixedHref)
  44. if (response.ok) {
  45. location.href = fixedHref
  46. await GM.setValue('notify', true)
  47. }
  48. }
  49. })()
  50.  
  51.