Enforce HTTPS

Automatically change HTTP URLs to HTTPS, when possible.

当前为 2023-05-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Enforce HTTPS
  3. // @namespace i2p.schimon.enforce-https
  4. // @description Automatically change HTTP URLs to HTTPS, when possible.
  5. // @author Schimon Jehudah, Adv.
  6. // @homepageURL https://openuserjs.org/scripts/sjehuda/Enforce_HTTPS
  7. // @supportURL https://openuserjs.org/scripts/sjehuda/Enforce_HTTPS/issues
  8. // @copyright 2023, Schimon Jehudah (http://schimon.i2p)
  9. // @license MIT; https://opensource.org/licenses/MIT
  10. // @run-at document-start
  11. // @include http://*
  12. // @connect self
  13. // @grant GM_xmlhttpRequest
  14. // @grant GM.xmlHttpRequest
  15. // @icon data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNjRtbSIgaGVpZ2h0PSI2NG1tIiB2aWV3Qm94PSIwIDAgNjQgNjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHRleHQgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgc3R5bGU9ImZvbnQtd2VpZ2h0OjQwMDtmb250LXNpemU6MTkycHg7bGluZS1oZWlnaHQ6MDt0ZXh0LWluZGVudDowO3RleHQtYWxpZ246c3RhcnQ7dGV4dC1kZWNvcmF0aW9uLXN0eWxlOnNvbGlkO3RleHQtZGVjb3JhdGlvbi1jb2xvcjojMDAwO3dyaXRpbmctbW9kZTpsci10YjtkaXJlY3Rpb246bHRyO3RleHQtb3JpZW50YXRpb246bWl4ZWQ7ZG9taW5hbnQtYmFzZWxpbmU6YXV0bztiYXNlbGluZS1zaGlmdDpiYXNlbGluZTt0ZXh0LWFuY2hvcjpzdGFydDtzaGFwZS1wYWRkaW5nOjA7c2hhcGUtbWFyZ2luOjA7aW5saW5lLXNpemU6MDtvcGFjaXR5OjE7ZmlsbDojMDAwO2ZpbGwtb3BhY2l0eToxO3N0cm9rZS13aWR0aDoxLjI3OTgyO3N0cm9rZS1saW5lY2FwOmJ1dHQ7c3Ryb2tlLWxpbmVqb2luOm1pdGVyO3N0cm9rZS1taXRlcmxpbWl0OjQ7c3Ryb2tlLWRhc2hvZmZzZXQ6MDtzdHJva2Utb3BhY2l0eToxO3N0b3AtY29sb3I6IzAwMDtzdG9wLW9wYWNpdHk6MSIgeD0iMTcuMDA1MjQ1IiB5PSIzMS42NTg0MDUiIHRyYW5zZm9ybT0idHJhbnNsYXRlKC00LjQzNjg1NjQgNDAuODk0OTQpIHNjYWxlKC4yNjQ1OCkiPjx0c3BhbiB4PSIxNy4wMDUyNDUiIHk9IjMxLjY1ODQwNSIgc3R5bGU9ImZvbnQtc2l6ZToxOTJweCI+8J+boe+4jzwvdHNwYW4+PC90ZXh0Pjwvc3ZnPgo=
  16. // @version 23.03
  17. // ==/UserScript==
  18.  
  19. /*
  20.  
  21. ## 🔐 Enforce HTTPS
  22. ### Redirect to HTTPS page
  23.  
  24. This userscript checks for obtainability of HTTPS version of a given page and redirects URL with HTTPS protocol.
  25.  
  26. ---
  27.  
  28. #### Recommended Userscripts
  29.  
  30. #### 🚫 [no-www](https://openuserjs.org/scripts/sjehuda/no-www)
  31. Correct URL to no-www URL.
  32.  
  33. ---
  34.  
  35. #### Please visit out counterparts
  36.  
  37. #### [HTTP to HTTPS](https://add0n.com/http-to-https.html)
  38. Automatically switched to secure HTTP version of webpages if possible.
  39.  
  40. #### [Smart HTTPS](https://mybrowseraddon.com/smart-https.html)
  41. Smart Change of HTTP addresses to the secure HTTPS.
  42.  
  43. ---
  44.  
  45. #### <!-- img src="https://raw.githubusercontent.com/KDE/falkon/master/logo.png" height="30" alt="Falkon" title="Download Falkon Browser"/ --> Help enhancing userscript support for <span style='color:MediumPurple'>Falkon web browser</span>
  46. C++ developers, help [Falkon](https://falkon.org/) web browser‬ to bring [better support](https://bugs.kde.org/show_bug.cgi?id=466533) for [GM.xmlHttpRequest](https://sourceforge.net/p/greasemonkey/wiki/GM_xmlhttpRequest/), so we would also enjoy this program.
  47.  
  48. _Thank you,
  49. Schimon Jehudah, Adv._
  50.  
  51. */
  52.  
  53. // https://github.com/makyen
  54. // https://github.com/greasemonkey/greasemonkey/issues/3160#issuecomment-1456758080
  55. const gmXmlhttpRequest = typeof GM_xmlhttpRequest === 'function' ? GM_xmlhttpRequest : GM.xmlHttpRequest;
  56.  
  57. if (!location.protocol.startsWith('http:')) return; // exit (else, continue)
  58.  
  59. var newURL = location.href.replace('http:','https:');
  60.  
  61. gmXmlhttpRequest({
  62. method: 'GET',
  63. url: newURL,
  64. onprogress: console.log('Checking for https...'),
  65. onload: function(response) {
  66. if (response.finalUrl == newURL) {
  67. location.href = newURL;
  68. } else {
  69. console.log('No https access available');
  70. }
  71. },
  72. onerror: function(response) {
  73. console.log('Error requesting for https')
  74. }
  75. })
  76.  
  77. /*
  78. fetch(newURL)
  79. .then((response) => {
  80. if (request.reseponseURL == newURL) {
  81. window.open(newURL,'_self');
  82. }
  83. })
  84. */