Combining Bookmarklets to create a toggle between

2019-04-12 11:16发布

By searching here, I now have two bookmarklets, which switch from/to HTTP/HTTPS :-

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

And ...

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

But is there a way, please, to combine them into a single bookmarklet, which will toggle from one to the other according to whichever is presently loaded?

3条回答
ゆ 、 Hurt°
2楼-- · 2019-04-12 11:48

This thread is a little old, but I haven't found a better answer anywhere else.

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

is fully formed bookmarklet code that should work in any browser.

查看更多
时光不老,我们不散
3楼-- · 2019-04-12 12:08
location.href.replace(/^http/i,"https").replace(/^http\w{2,}/i,"http")
查看更多
贼婆χ
4楼-- · 2019-04-12 12:09
if (window.location.protocol == 'http:') window.location.protocol = 'https:'
else window.location.protocol = 'http:'

Then all you'd need to do is format that as a bookmarklet. Enjoy!

查看更多
登录 后发表回答