javascript
  • greasemonkey
  • userscripts
  • 2011-05-02 157 views 1 likes 
    1

    我正在制作一个Facebook的用户脚本,这将有助于使用谷歌翻译Api翻译文本。脚本将html和css内容成功注入Facebook。在userscript中使用谷歌翻译api

    问题与谷歌翻译Api。我正在注入脚本标记

    var s = document.createElement('script'); 
    s.type="text/javascript"; 
    s.src='https://www.google.com/jsapi?key=AIzaSyD24A-czAdTj8pPc5ugo0bYiPRx8Rc2pXo'; 
    document.body.appendChild(s); 
    

    此脚本首先加载该URL 2或3次。

    实际使用语言API我注入另一个脚本标签

    var ldfun = document.createElement('script'); 
    ldfun.setAttribute('type', 'application/javascript'); 
    ldfun.textContent= "google.load('language','1');"; 
    document.body.appendChild(ldfun); 
    

    这个脚本没有运行,有时然后运行页面naviages路程。

    请帮助

    +0

    我假设你在谈论Greasemonkey用户脚本? – Raze 2011-05-02 06:04:43

    回答

    0

    确保脚本不iFrame中运行,然后利用一个延迟来确保JS加载库(否则,浏览器将运行这些2 script小号异步)

    东西像这样:

    if (window.top != window.self) //-- Don't run on frames or iframes 
        return; 
    
    function addJS_Node (text, s_URL) 
    { 
        var scriptNode      = document.createElement ('script'); 
        scriptNode.type      = "text/javascript"; 
    
        if (text) scriptNode.textContent = text; 
        if (s_URL) scriptNode.src   = s_URL; 
    
        //--- document.head is best. Use document.body only on poor target pages. 
        document.head.appendChild (scriptNode); 
    } 
    
    //--- Load Google-Translate, JS API. 
    addJS_Node (null, 'https://www.google.com/jsapi?key=AIzaSyD24A-czAdTj8pPc5ugo0bYiPRx8Rc2pXo'); 
    
    //--- Initialize Google-Translate after a delay to make sure it has loaded. 
    setTimeout (function() { 
        addJS_Node ("google.load('language','1');", null); 
        }, 
        1500 
    ); 
    
    +0

    这工作,但现在当google.load执行页面实际导航到该页面。如何阻止它导航? – anil90 2011-05-03 00:41:31

    +0

    我不知道,我无法复制这个问题。使用Ajax调用转换器工具可能会有更好的运气。每次加载这个笨重的库都会带来更少的开销和延迟。 – 2011-05-04 00:02:18

    +0

    好吧,我会尝试Greasemonkey AJAX API谢谢 – anil90 2011-05-04 00:58:18

    相关问题