简体   繁体   中英

How to replace http links to https in whole page using Greasemonkey?

Is there anyway to replace any http link to https in whole page ? there are some scripts in userscripts.org but they only redirect the url and they don't change html content ..

Thanks

If you are concerned about security and privacy, you are much better off installing and using an extension like HTTPS Everywhere .

An extension has more power to enforce SSL on: links, images, video and sound files, CSS and javascript files, flash objects, AJAX calls, etc. Whereas a Greasemonkey script, or userscript, can have a devil of a time doing just part of that.

But, if you really just want to change just the links ( <a> nodes) in a page, that is not too hard to do. The biggest thing to account for is sites that add links via AJAX. For that reason, use jQuery and waitForKeyElements() to handle all the links.

Here's a complete script to get you started:

// ==UserScript==
// @name     _Remap links to https
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements ("a", remapToSSL);

function remapToSSL (jNode) {
    var node    = jNode.get (0);

    if (node.protocol === "http:") {
        node.protocol = "https:";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM