Enforce HTTPS

Automatically change HTTP URLs to HTTPS, when possible.

目前為 2023-06-01 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

/*

## 🔐 Enforce HTTPS
### Redirect to HTTPS page

This userscript checks for obtainability of HTTPS version of a given page and redirects URL with HTTPS protocol.

---

#### Recommended Userscripts

#### 🚫 [no-www](https://openuserjs.org/scripts/sjehuda/no-www)
Correct URL to no-www URL.

---

#### Please visit out counterparts

#### [HTTP to HTTPS](https://add0n.com/http-to-https.html)
Automatically switched to secure HTTP version of webpages if possible.

#### [Smart HTTPS](https://mybrowseraddon.com/smart-https.html)
Smart Change of HTTP addresses to the secure HTTPS.

---

#### <!-- 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>
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.

_Thank you,
Schimon Jehudah, Adv._

*/

// ==UserScript== 
// @name        Enforce HTTPS
// @namespace   i2p.schimon.enforce-https
// @description Automatically change HTTP URLs to HTTPS, when possible.
// @author      Schimon Jehudah, Adv.
// @homepageURL https://greasyfork.org/en/scripts/465953-enforce-https
// @supportURL  https://greasyfork.org/en/scripts/465953-enforce-https/feedback
// @copyright   2023, Schimon Jehudah (http://schimon.i2p)
// @license     MIT; https://opensource.org/licenses/MIT
// @run-at      document-start
// @match       http://*/*
// @connect     self
// @grant       GM_xmlhttpRequest
// @grant       GM.xmlHttpRequest
// @icon        data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNjRtbSIgaGVpZ2h0PSI2NG1tIiB2aWV3Qm94PSIwIDAgNjQgNjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHRleHQgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgc3R5bGU9ImZvbnQtd2VpZ2h0OjQwMDtmb250LXNpemU6MTkycHg7bGluZS1oZWlnaHQ6MDt0ZXh0LWluZGVudDowO3RleHQtYWxpZ246c3RhcnQ7dGV4dC1kZWNvcmF0aW9uLXN0eWxlOnNvbGlkO3RleHQtZGVjb3JhdGlvbi1jb2xvcjojMDAwO3dyaXRpbmctbW9kZTpsci10YjtkaXJlY3Rpb246bHRyO3RleHQtb3JpZW50YXRpb246bWl4ZWQ7ZG9taW5hbnQtYmFzZWxpbmU6YXV0bztiYXNlbGluZS1zaGlmdDpiYXNlbGluZTt0ZXh0LWFuY2hvcjpzdGFydDtzaGFwZS1wYWRkaW5nOjA7c2hhcGUtbWFyZ2luOjA7aW5saW5lLXNpemU6MDtvcGFjaXR5OjE7ZmlsbDojMDAwO2ZpbGwtb3BhY2l0eToxO3N0cm9rZS13aWR0aDoxLjI3OTgyO3N0cm9rZS1saW5lY2FwOmJ1dHQ7c3Ryb2tlLWxpbmVqb2luOm1pdGVyO3N0cm9rZS1taXRlcmxpbWl0OjQ7c3Ryb2tlLWRhc2hvZmZzZXQ6MDtzdHJva2Utb3BhY2l0eToxO3N0b3AtY29sb3I6IzAwMDtzdG9wLW9wYWNpdHk6MSIgeD0iMTcuMDA1MjQ1IiB5PSIzMS42NTg0MDUiIHRyYW5zZm9ybT0idHJhbnNsYXRlKC00LjQzNjg1NjQgNDAuODk0OTQpIHNjYWxlKC4yNjQ1OCkiPjx0c3BhbiB4PSIxNy4wMDUyNDUiIHk9IjMxLjY1ODQwNSIgc3R5bGU9ImZvbnQtc2l6ZToxOTJweCI+8J+boe+4jzwvdHNwYW4+PC90ZXh0Pjwvc3ZnPgo=
// @version     23.06
// ==/UserScript==

// https://github.com/makyen
// https://github.com/greasemonkey/greasemonkey/issues/3160#issuecomment-1456758080
const gmXmlhttpRequest = typeof GM_xmlhttpRequest === 'function' ? GM_xmlhttpRequest : GM.xmlHttpRequest;

if (!location.protocol.startsWith('http:')) return; // exit (else, continue)

var newURL = location.href.replace('http:','https:');

gmXmlhttpRequest({
  method: 'GET',
  url: newURL,
  onprogress: console.log('Checking for https...'),
  onload: function(response) {
    if (response.finalUrl == newURL) {
      location.href = newURL;
    } else {
      console.log('No https access available');
    }
  },
  onerror: function(response) {
    console.log('Error requesting for https')
  }
})

/*
fetch(newURL)
  .then((response) => {
     if (request.reseponseURL == newURL) {
       window.open(newURL,'_self');
     }
  })
*/