代码之家  ›  专栏  ›  技术社区  ›  Radek Simko

在Chrome扩展中使用远程JavaScript

  •  1
  • Radek Simko  · 技术社区  · 14 年前

    我的manifest.json如下所示:

    {
        "name": "My Extension",
        "version": "0.1",
        "content_scripts": [
        {
            "matches": ["http://*/*"],
            "js": ["jquery.js", "main.js"],
            "run_at": "document_end",
            "all_frames": true
        }
        ]
    }
    

    我想使用一个JavaScript API,它受所选域的使用限制,所以我 只需将其插入Chrome中的加载页面,如下所示:

    $('head').append('<script src="http://example.com/myApi.js?key=%key%"></script>');
    

    但我没有任何线索,如何解决这个问题-把远程JS文件放在哪里才能使用它。

    "virtual DOM"

    在哪里插入代码?一些背景页??

    3 回复  |  直到 7 年前
        1
  •  0
  •   onsy    14 年前

    尝试以下脚本:

    if(window.top == window && !document.getElementById('molseek'))
    {
        if (document && document.doctype && document.doctype.name && document.doctype.name.toLowerCase() == 'html') {
            loadToolTip();
        }
        // Weird guest... but if we got an head element, try to validate even if no doctype...
        else if (document && document.getElementsByTagName('head').length) {
            loadToolTip();
        }
        // Weird guest #2... but if we got title element, try to validate even if no doctype nor head...
        else if (document && document.getElementsByTagName('title').length) {
            loadToolTip();
        }
    }
    
    function loadToolTip(){
    
        (function(s){
            if(!window.molseek){
                s.src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js';
                (document.getElementsByTagName("head").item(0)||document.body).appendChild(s)
            }
        })(document.createElement('script'));
        (function(s){
            if(!window.apture){
                s.src='https://location';
                (document.getElementsByTagName("head").item(0)||document.body).appendChild(s)
            }
        })(document.createElement('script'));
    
    }
    
        2
  •  3
  •   Shikiryu DhruvJoshi    14 年前

    你试过:

    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'http://example.com/myApi.js?key=%key%';
    document.getElementsByTagName('head')[0].appendChild(script); 
    

    我在我的googlechrome扩展中使用了它,效果很好。

        3
  •  0
  •   Devin Rhode    13 年前