gpt4 book ai didi

javascript - 在 XUL 窗口上添加加一个按钮时出现问题

转载 作者:行者123 更新时间:2023-11-30 18:41:17 25 4
gpt4 key购买 nike

我试图在 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.js ( bootstrap-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

最佳答案

@systempuntoout 是正确的,理论上,将 iframe 类型属性设置为“内容”应该可以解决此问题。然而,我过去遇到过这方面的问题。它可能有效,但我认为 XUL 在这方面有点问题。如果是我,我会嵌入一个 XUL 元素而不是 XUL <iframe> 并将静态 HTML 页面加载到浏览器中(即通过调用 browser.loadURI),其中包含我要运行的代码(在本例中src 设置为“https://plusone.google.com...”的 iframe。这样您的代码将作为真实内容运行,就像在主浏览器内容窗口中一样。

您可以将 HTML 页面写入磁盘(因为部分 iframe 源是动态生成的),然后使用 file://URL 引用它。由于在这种情况下代码会非常短,您甚至可以尝试使用数据 URI,这样您就不必将临时文件写入磁盘。

换句话说,您将创建一个 HTML 文件(在内存中或磁盘上):

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

然后您将创建一个 元素,就像您现在为 iframe 所做的那样,将其插入您的 XUL 文档并对其调用 loadURI(),引用 HTML 文件(通过 file: 或 data: URI)。

关于javascript - 在 XUL 窗口上添加加一个按钮时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6806714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com