强制JavaScript通过https加载其他资源

时间:2016-07-02 05:43:47

标签: javascript

我在当前项目中使用Google的Virtual Keyboard (Deprecated)。不幸的是,它从一个不安全的来源加载一些额外的js资源。有没有办法强制脚本使用https而不是http?

问题是用于显示正确字母的语言文件。样式表,例如通过https加载。

这是我的剧本:

()

更新

在普通http上加载的唯一资源是the language-file,在本例中是瑞典语版本。 语言文件在<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Virtual Keyboard | Google APIs</title> <script type="text/javascript" src="https://www.google.com/jsapi"></script> </head> <body> <input id="language"> <script> google.load("elements", "1", { packages: "keyboard" }); function onLoad() { var kbd1 = new google.elements.keyboard.Keyboard( [google.elements.keyboard.LayoutCode.SWEDISH], ['language']); } google.setOnLoadCallback(onLoad); </script> </body> </html> 期间加载到函数onLoad中。

2 个答案:

答案 0 :(得分:2)

基于answer to another question 似乎可以重新定义用于加载的src元素的<script>属性 swedisch键盘的javascript代码。如果确保执行以下代码 在new google.elements.keyboard.Keyboard来电之前,http将被https取代。 从chrome调试控制台中的网络信息来看,这确实似乎加载了键盘 通过https设置。

Object.defineProperty(HTMLScriptElement.prototype, 'src', {
    get: function() {
        return this.getAttribute('src')
    },
    set: function(url) {
        var prefix = "http://";

        if (url.startsWith(prefix))
            url = "https://" + url.substr(prefix.length);

        console.log('being set: ' + url);
        this.setAttribute('src', url);
    }
});

答案 1 :(得分:-1)

只需像这样设置你的脚本网址,它就可以了!

<script src="//www.google.com/jsapi"></script>

这是url relative protocol 类型&#34; javascript&#34;部分不再是必要的,因为它被视为javascript如果没有指定,所以节省空间! MDN element script