我正在开发一个用户脚本,它将不安全的页面(HTTP)重定向到https: version。
// ==UserScript==
// @name Secure Redirect
// @namespace http://tampermonkey.net/
// @version 0.1
// @description A simple userscript that redirects pages to https:.
// @author You
// @match *
// @grant none
// ==/UserScript==
(function() {
'use strict';
window.onload=()=>{
if (location.protocol != "https:") {
window.location.href = "https:" + window.location.href.substring(window.location.protocol.length);
}
};
})();请告诉我它是否有效,以及我能改进什么。我对这个项目的GitHub回购是这里。
发布于 2018-10-01 06:22:55
addEventListener方法按照推荐的方式附加事件侦听器--您真的需要执行重定向代码吗?protocol)(function (w) {
'use strict';
var secureProtocol = 'https';
if ( !w.location.protocol.startsWith(secureProtocol) ) {
w.location.href = w.location.href.replace(/^[^:]+/, secureProtocol);
}
})(window);https://codereview.stackexchange.com/questions/204632
复制相似问题