使用Chrome扩展程序将http重定向到https

Gol*_*den 1 ssl https google-chrome localhost google-chrome-extension

我正在开发一些使用https服务器的Node.js应用程序.在我开发它们时,我localhost使用自签名证书运行它们.基本上,一切都有效,但我有两个问题:

  1. 当我https://localhost:3000第一次将浏览器指向时,它会警告我有关不可信任的证书.当然,这是真实的(也是重要的),但是在开发过程中它是不合适的.当然,我可以将证书添加为可信证书,但它们会不时更改,我不想让证书存储混乱.

  2. 有时我只是忘记将https部分输入地址栏,因此Chrome会尝试使用加载网站http.无论出于何种原因,Chrome都没有意识到没有网络服务器响应http请求,而是加载和加载并加载......

我想要解决这两个问题的方法是创建一个位于地址栏旁边的Chrome扩展程序,并提供一个按钮,您可以使用该按钮切换其状态:

  • 如果禁用扩展,则它什么都不做.
  • 如果扩展已启用,并且您发送请求localhost(并且只有!),它将执行两项操作:
    1. 如果请求使用http,但页面仍然pending在几秒钟后,它将尝试使用https.
    2. 无论浏览器是否信任,都暂时接受任何证书.

明确说明:这些规则仅适用于localhost.

那么,现在我的问题是:

  • 是否可以使用Chrome扩展程序?
  • 由于我完全没有编写Chrome扩展程序的经验,这将是一个很好的起点,我应该在Google上寻找哪些条款?

gka*_*pak 5

谷歌浏览器扩展程序文档是一个伟大的地方开始.除了 "证书接受"部分之外,使用Chrome扩展程序可以描述您描述的所有内容.(我不是说这是不可能的,我只是不知道是不是 - 但如果是的话,我会非常惊讶(并且担心).)

当然,总是存在--ignore-certificate-errors命令行开关,但它不会区分localhost其他域.

如果您决定实现其余功能,我建议首先查看chrome.tabs和/或chrome.webRequest.(我还要提及"内容脚本"不太可能有用.)


也就是说,下面是一些演示扩展的代码(只是为了让你入门).

做什么:
当取消激活 - >没有
激活时 - >监听被定向到URL的标签http://localhost[:PORT][/...]并将其重定向到https(它不等待响应或其他任何内容,它只是立即重定向它们).

使用方法:
单击浏览器操作图标以激活/取消激活.

当然,这不是完美/完整,但它是一个起点:)


扩展目录结构:

        extention-root-directory/
         |_____ manifest.json
         |_____ background.js
         |_____ img/
                 |_____ icon19.png
                 |_____ icon38.png
Run Code Online (Sandbox Code Playgroud)

manifest.json :(
有关可能字段的详细信息,请参见此处.)

{
    "manifest_version": 2,
    "name":    "Test Extension",
    "version": "0.0",
    "default_locale": "en",
    "offline_enabled": true,
    "incognito":       "split",

    // The background-page will listen for
    // and handle various types of events
    "background": {
        "persistent": false,   // <-- if you use chrome.webRequest, 'true' is required
        "scripts": [
            "background.js"
        ]
    },

    // Will place a button next to the address-bar
    // Click to enable/disable the extension (see 'background.js')
    "browser_action": {
        "default_title": "Test Extension"
        //"default_icon": {
        //    "19": "img/icon19.png",
        //    "38": "img/icon38.png"
        //},
    },

    "permissions": [
        "tabs",                  // <-- let me manipulating tab URLs
        "http://localhost:*/*"   // <-- let me manipulate tabs with such URLs 
    ]
}
Run Code Online (Sandbox Code Playgroud)

background.js :(
相关文档:后台页面,事件页面,浏览器操作,chrome.tabs API)

/* Configuration for the Badge to indicate "ENABLED" state */
var enabledBadgeSpec = {
    text: " ON ",
    color: [0, 255, 0, 255]
};
/* Configuration for the Badge to indicate "DISABLED" state */
var disabledBadgeSpec = {
    text: "OFF",
    color: [255, 0, 0, 100]
};


/* Return whether the extension is currently enabled or not */
function isEnabled() {
    var active = localStorage.getItem("active");
    return (active && (active == "true")) ? true : false;
}

/* Store the current state (enabled/disabled) of the extension
 * (This is necessary because non-persistent background pages (event-pages)
 *  do not persist variable values in 'window') */
function storeEnabled(enabled) {
    localStorage.setItem("active", (enabled) ? "true" : "false");
}

/* Set the state (enabled/disabled) of the extension */
function setState(enabled) {
    var badgeSpec = (enabled) ? enabledBadgeSpec : disabledBadgeSpec;
    var ba = chrome.browserAction;
    ba.setBadgeText({ text: badgeSpec.text });
    ba.setBadgeBackgroundColor({ color: badgeSpec.color });
    storeEnabled(enabled);
    if (enabled) {
        chrome.tabs.onUpdated.addListener(localhostListener);
        console.log("Activated... :)");
    } else {
        chrome.tabs.onUpdated.removeListener(localhostListener);
        console.log("Deactivated... :(");
    }
}

/* When the URL of a tab is updated, check if the domain is 'localhost'
 * and redirect 'http' to 'https' */
var regex = /^http(:\/\/localhost(?::[0-9]+)?(?:\/.*)?)$/i;
function localhostListener(tabId, info, tab) {
    if (info.url && regex.test(info.url)) {
        var newURL = info.url.replace(regex, "https$1");
        chrome.tabs.update(tabId, { url: newURL });
        console.log("Tab " + tabId + " is being redirected to: " + newURL);
    }
}

/* Listen for 'browserAction.onClicked' events and toggle the state  */
chrome.browserAction.onClicked.addListener(function() {
    setState(!isEnabled());
});

/* Initially setting the extension's state (upon load) */
setState(isEnabled());
Run Code Online (Sandbox Code Playgroud)