Javascript 结合Bookmarklets在HTTP和HTTPS之间创建切换?

Javascript 结合Bookmarklets在HTTP和HTTPS之间创建切换?,javascript,http,url,https,bookmarklet,Javascript,Http,Url,Https,Bookmarklet,通过在此处搜索,我现在有两个bookmarklet,可以从/切换到HTTP/HTTPS:- javascript:location=location.href.replace(/http:/g,"https:") 而且 javascript:location=location.href.replace(/https:/g,"http:") 但是,有没有一种方法可以将它们组合成一个bookmarklet,根据当前加载的书签从一个书签切换到另一个书签 location.href.replace(

通过在此处搜索,我现在有两个bookmarklet,可以从/切换到HTTP/HTTPS:-

javascript:location=location.href.replace(/http:/g,"https:")
而且

javascript:location=location.href.replace(/https:/g,"http:")
但是,有没有一种方法可以将它们组合成一个bookmarklet,根据当前加载的书签从一个书签切换到另一个书签

location.href.replace(/^http/i,"https").replace(/^http\w{2,}/i,"http")

然后,您需要做的就是将其格式化为bookmarklet。享受吧

这条线索有点旧了,但我在其他任何地方都没有找到更好的答案

javascript:((function(){window.location=location.href.replace(/^http/i,“https”).replace(/^http\w{2,}/i,“http”);})()


是完全格式的bookmarklet代码,可以在任何浏览器中使用。

javascript:location=location.href.replace(/^http/i,“https”).replace(/^http\w{2,}/i,“http”)
在Firefox上测试,但在Chrome上失败。@Batandwa尝试类似于
javascript:void(…)
的方法。
if (window.location.protocol == 'http:') window.location.protocol = 'https:'
else window.location.protocol = 'http:'