2011-07-24 29 views
1

我試圖添加加上XUL窗口上的一個按鈕,但我得到這個錯誤:問題將加上XUL窗口中的一個按鈕

Error: Permission denied for <https://plusone.google.com> to call method ChromeWindow.postMessage 
Source file: https://ssl.gstatic.com/webclient/js/gc/22431124-0a127465/googleapis.client__plusone.js 
Line: 14 

我加入了一個iframe來https://plusone.google.com/u/0/_/+1/fastbutton?url= ...

有沒有辦法在XUL窗口中添加+1按鈕並使其接受postMessage


我試圖發展的插件是在圖像的下面。唯一的問題是它不會因爲許可而註冊點擊。

+1 any

bootstrap.jsbootstrap-vsdoc.js

/// <reference path="bootstrap-vsdoc.js" /> 

/// <summary> 
/// Made by Bruno Leonardo Michels 
/// </summary> 

var watcher = Components.classes["@mozilla.org/embedcomp/window-watcher;1"] 
         .getService(Components.interfaces.nsIWindowWatcher); 

var listenOpen = { 
    observe : function(cWindow, cEvent) { 
     if (cEvent != "domwindowopened") return; 
      cWindow.addEventListener("load", start, false); 
    } 
}; 

function startup(data, reason) { 
    watcher.registerNotification(listenOpen); 

    var mWindows = watcher.getWindowEnumerator(); 
    while (mWindows.hasMoreElements()) { 
     start(mWindows.getNext()); 
    } 
} 
function shutdown(data, reason) { 
    watcher.unregisterNotification(listenOpen); 

    var mWindows = watcher.getWindowEnumerator(); 
    while (mWindows.hasMoreElements()) { 
     end(mWindows.getNext()); 
    } 
} 
function install(data, reason) { 

} 
function uninstall(data, reason) { 

} 

/// #region Methods 

function getWindow(cWindow) 
{ 
    try 
    { 
     if (cWindow instanceof Components.interfaces.nsIDOMEvent) 
     { 
      cWindow = cWindow.currentTarget; 
     } 
     if (cWindow.document.documentElement.getAttribute("windowtype") != "navigator:browser") 
      return; 
    } 
    catch(ex) { } 

    return cWindow; 
} 

function ajaxGet(cWindow, url, cb) 
{ 
    var xmlhttp; 
    xmlhttp = new cWindow.XMLHttpRequest(); 
    xmlhttp.open("GET", url, true); 
    xmlhttp.onreadystatechange=function() 
    { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
     { 
      cb(xmlhttp); 
     } 
    }; 
    xmlhttp.send(); 
} 

var eventList = []; 
function bind(gBrowser, cWindow, target, eventName, fn) 
{ 
    var ev = function(e) { fn(gBrowser, cWindow, e); }; 
    eventList.push(ev); 
    target.addEventListener(eventName, eventList[eventList.length-1], false); 
} 
function unbind(target, eventName, fn) 
{ 
    var b = target.removeEventListener ? 
     function(elem, type, handle) { 
      if (elem.removeEventListener) { 
       elem.removeEventListener(type, handle, false); 
      } 
     } : 
     function(elem, type, handle) { 
      if (elem.detachEvent) { 
       elem.detachEvent("on" + type, handle); 
      } 
     }; 

    b(target, eventName, fn); 
} 
function unbindAll(target, eventName) 
{ 
    for (var i in eventList) 
    { 
     unbind(target, eventName, eventList[i]); 
    } 
} 

/// #endregion 

/// #region Events 

function start(cWindow) { 
    cWindow = getWindow(cWindow); 

    if (!cWindow) return; 

    with (cWindow) 
    { 
     bind(gBrowser, cWindow, gBrowser.tabContainer, "TabAttrModified", tabChange); 

     var window = cWindow; 
     var document = cWindow.document; 

     var url = window.location.href; 
     if (!/^http/i.test(url))url="http://www.orkutmanager.net/"; 
     var urlE= window.encodeURIComponent(url); 

     var iconsBar = document.getElementById("urlbar-icons"); 

     function insScript(w) 
     { 
      var sc = document.createElement("script"); 
      sc.src = "https://apis.google.com/js/plusone.js"; 
      sc.type= "text/javascript"; 
      sc.setAttribute("extension", "plusoneany"); 
      (document.lastChild).appendChild(sc); 
     } 
     insScript(this); 
     insScript(this.parent); 
     insScript(this.top); 

     var button = document.createElement("iframe"); 
     button.id = "extensions.plusoneany.button"; 
     button.setAttribute("src", "https://plusone.google.com/u/0/_/+1/fastbutton?url=" + urlE + 
      "&size=small&count=true&hl=en-US&_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe"); 
     button.setAttribute("class", "urlbar-icon extensions-plusoneany"); 
     button.setAttribute("style", "border:0;padding:0;margin:0;width:70px;height:16px;"); 

     iconsBar.insertBefore(button, iconsBar.lastChild); 
    } 
} 
function end(cWindow) { 
    try 
    { 
     unbindAll(gBrowser.tabContainer, "TabAttrModified"); 
    } 
    catch(ex){} 

    try 
    { 
     var elements = cWindow.document.getElementsByClassName("extensions-plusoneany"); 

     for (var i in elements) 
     { 
      elements[i].parentNode.removeChild(elements[i]); 
     } 
    } 
    catch(ex){} 
} 

function tabChange(gBrowser, cWindow, e) { 
    var win = gBrowser.selectedBrowser.contentWindow; 
    var uns = gBrowser.selectedBrowser.contentWindow.wrappedJSObject; 

    uns.clearTimeout(uns.PlusOneAnyTimeout); 

    uns.PlusOneAnyTimeout = uns.setTimeout(function() { 
     var url = win.location.href; 
     if (!/^http/i.test(url))url="http://www.orkutmanager.net/"; 
     var urlE= uns.encodeURIComponent(url); 

     try { 
      var ifr = cWindow.document.getElementById("extensions.plusoneany.button"); 
      ifr.setAttribute("src", "https://plusone.google.com/u/0/_/+1/fastbutton?url=" + urlE + 
       "&size=small&count=true&hl=en-US&_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe"); 
     }catch(ex){} 
    }, 500); 
} 

/// #endregion 
+0

你有沒有試圖強迫[類型](https://developer.mozilla.org/en/XUL/Attribute/browser.type )內容/鉻? – systempuntoout

回答

1

@systempuntoout是正確的,從理論上說,設置了iframe type屬性爲「內容」應該解決這個問題。不過,我過去遇到過這個問題。它可能工作,但我認爲XUL在這方面有點兒麻煩。如果是我,我會嵌入一個XUL < browser>元素而不是XUL < iframe>並將一個靜態HTML頁面加載到瀏覽器中(即通過調用browser.loadURI),其中包含我想要運行的代碼(在這種情況下src設置爲「https://plusone.google.com ...」的iframe)。這樣,您的代碼將以真實內容的形式運行,就像它在主瀏覽器內容窗口中一樣。

您可以編寫HTML頁面到磁盤(因爲IFRAME源的一部分是動態生成的),然後用一個文件中引用它:// URL。由於在這種情況下代碼很短,您甚至可以嘗試使用數據URI,這將使您無需將臨時文件寫入磁盤。

換句話說,你會創建一個HTML文件(在內存或磁盤上)與:

<html> 
<body> 
<iframe src="https://plusone.google.com..." /> 
</body> 
</html> 

然後你創建就像你的iframe,現在做一個<瀏覽器>元素,插入它放到你的XUL文檔中並調用loadURI(),引用HTML文件(通過file:或data:URI)。

+0

謝謝。如果你想現在使用這個插件... http://goo.gl/1FiKv :) – BrunoLM

0

邪惡無比的答案,但是你不能只是得到https://apis.google.com/js/plusone.js的內容,然後EVAL呢?這是谷歌,有什麼可能出錯;]

1

我想嘗試指定type加入這一行:

ifr.setAttribute("type","content"); 
+0

感謝您的提示!有效。 – BrunoLM