gpt4 book ai didi

javascript - js的xul容器函数,我想从页面上的外部iframe调用js函数之一

转载 作者:行者123 更新时间:2023-12-04 04:47:58 36 4
gpt4 key购买 nike

我建立了xul模板,并在xul中添加了js。

主页包含一个iframe广告代码。
我想从加载的iframe页面调用特定的js函数(在xul内)。

例如:

这是xul文件:

<?xml version="1.0"?>


         <script type="application/x-javascript"
src="chrome://tuttoolbar/content/general.js" />

<toolbox id="navigator-toolbox">

<toolbar id="TutTB-Toolbar" toolbarname="Tutorial Toolbar" accesskey="T"
class="chromeclass-toolbar" context="toolbar-context-menu"
hidden="false" persist="hidden">

<toolbarbutton id="TutTB-MainMenu" type="menu"
tooltiptext="Tutorial Toolbar Main Menu">
<menupopup>
<menuitem label="Google Home Page" accesskey="G"
tooltiptext="Navigate to Google"
oncommand="countChild('rso','li')" />

<menuseparator />

<menuitem label="Born Geek Website" accesskey="B"
tooltiptext="Navigate to Born Geek"
oncommand="te()" />
</menupopup>
</toolbarbutton>


</toolbar>



</toolbox>

这是General JS文件(在xul文件中):
function box(){
alert("Box Work");
}

test.php-这是在iframe中加载的源页面:
     <html>
<head>
</head>
<body>
<div id="call" onclick="javascript:box();">
</body>
</html>

这是主文件:
    <html>
<head>
</head>
<body>
<iframe src="test.php" width="400" height="40"></iframe>
</body>
</html>

有什么办法吗?

最佳答案

内容(不受信任)文档(包括任何IFRAME)不能直接运行扩展或XulRunner加载的其他特权代码中定义的功能。即使您的特权代码将功能分配给了不受信任的内容窗口,一旦该功能尝试使用/调用特权内容,您也会收到安全错误。

但是,您可以让特权代码将事件侦听器添加到不受信任的内容窗口中!稍作努力,您就可以建立一个方案,以轻松地将“特权”功能安装到不受信任的内容中-即:突破安全沙箱。

这是一个允许不受信任的domWindow执行(特权)跨域HTTP GET操作的示例:

/* 
Give the end-user this API:
xdomainGET(sURL,
function(
iHTTPStatusCode,
sHTTPStatusText,
sHTTPResponseText
) { ... })
*/

function install_xdomainGET_on_my_special_page(chromeWindow, domWindow) {

install_privileged_method(
/* this is the current browser chromeWindow (your XUL overlay is here!) */
chromeWindow,

/* this is the unstrusted domWindow containing your IFRAME */
domWindow,

/* the name of the method we're adding to the untrusted domWindow */
'xdomainGET',

/* This is the methodFactory */
function(priv) {
/* This is the method that will be installed onto the untrusted
domWindow. It takes two arguments, 'sURL' and 'cb'. 'sURL' is a
cross-domain HTTP URL which we want to be able to GET from the
untrusted domWindow. 'cb' is a callback function which returns
3 values to the caller... */
return function(sURL, cb) {
priv.call([sURL], function(rstatus, rdata, rerror){
cb(rstatus ? rdata.statusCode : 0, // iHTTPStatusCode
rstatus ? rdata.statusText : rerror, // sHTTPStatusText
rstatus ? rdata.responseText : null); // sHTTPResponseText
});
};
},

/* This is the privileged 'handler code. It has access to the
chromeWindow, and all of the privileged APIs that are available
there. (e.g: nsI*, XPCom, js-ctypes, etc) */
function (chromeWindow, args, cb) {
var [url] = args;
var xhr = new chromeWindow.XMLHttpRequest();
xhr.onload = function(){
cb(1, {
statusCode: this.status,
statusText: this.statusText,
responseText: this.responseText
}, null);
};
xhr.addEventListener('error', function(error_evt) {
cb(0, null, error_evt.error);
}, false);
xhr.open('get', url, true);
xhr.send();
}
);
}

这是我的库函数,使之成为可能。基本上,它做两件事(来自特权代码):
  • 它将一个函数安装到不受信任的domWindow中,该函数为最终用户提供预期的API,在调用该函数时,打包参数(包括回调函数)并引发综合事件。它将其组织为“方法工厂”,以便最终用户可以使用他们想要的任何形状的API(只要需要回调即可!)。
  • 它将事件处理程序安装在不受信任的窗口上。当不受信任的窗口调度其综合事件时,事件处理程序将捕获该事件,将参数解压缩以执行提供的特权代码。然后,它将调用事件详细信息中提供的不受信任的回调函数。

  • const Cc = Components.classes;
    const Ci = Components.interfaces;
    let consoleService = Cc["@mozilla.org/consoleservice;1"]
    .getService(Ci.nsIConsoleService);
    function LOG(msg) {
    consoleService.logStringMessage("EXTENSION: "+msg);
    }

    // install a privileged method on the given (untrusted) 'target' window
    var install_privileged_method = (function(){
    var gensym = (function (){
    var __sym = 0;
    return function () { return '__sym_'+(__sym++); }
    })();

    return function (chromeWindow, target, slot, handler, methodFactory) {
    try {

    // initialise target window with ticket/callback-dict/namespace
    target.__pmcache__ = target.hasOwnProperty('__pmcache__')
    ? target.__pmcache__
    : { ticket_no: 0, callbacks: {}, namespace: gensym() };

    // install the user factory-generated dispatcher method on
    // the 'target' untrusted content window.
    target[slot] = methodFactory({ call: function(fargs, fcb) {
    try {
    var ticket_no = target.__pmcache__.ticket_no++;
    target.__pmcache__.callbacks[ticket_no] = fcb;
    var cevent = target.document.createEvent("CustomEvent");
    cevent.initCustomEvent(
    target.__pmcache__.namespace+'.'+slot,
    true, true, { fargs: fargs, ticket_no: ticket_no }
    );
    target.dispatchEvent(cevent);
    } catch (ue) {
    fcb(0, null, 'untrusted dispatcher error: '+ue);
    }
    }});
    LOG("installed untrusted dispatcher for method '"+slot+"'.");

    // Add an event listener to (untrusted) target window which
    // listens for custom event generated by above dispatcher method.
    target.addEventListener(
    target.__pmcache__.namespace+'.'+slot,
    function(cevent){
    var ticket_no = cevent.detail.ticket_no;
    var fargs = cevent.detail.fargs;
    var fcb = target.__pmcache__.callbacks[ticket_no];
    try {
    handler(chromeWindow, fargs, fcb);
    } catch (pe) {
    fcb(0, null, 'privileged handler error: '+pe);
    }
    },
    false,
    true
    );
    LOG("installed privileged handler for method '"+slot+"'.");

    } catch (ie) {
    LOG("ERROR installing handler/factory for privileged "+
    "method '"+slot+"': "+ie);
    }
    };
    })();

    现在,我们已经找到了一种突破沙箱的方法,我们需要确保仅将这种可能性添加到“受信任”的网页中。 (即:您托管PHP的网址)。

    我一直在Firefox中进行此操作,因此我正在处理用户界面中的一个或多个 TabBrowser XUL元素。当您使用XulRunner时,情况可能并非如此。

    为了找到我们的“受信任”页面,我们需要查看所有当前(以及将来)的chromeWindows并在它们上安装“加载”处理程序。
    let wm = Cc["@mozilla.org/appshell/window-mediator;1"]
    .getService(Ci.nsIWindowMediator);
    let windows = wm.getEnumerator("navigator:browser");
    while (windows.hasMoreElements()) {
    let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
    WindowListener.setupBrowserUI(domWindow);
    }
    wm.addListener(WindowListener);
    WindowListener定义为:
    var WindowListener = {
    setupBrowserUI: function(window, xulWindow, othWindow) {
    window.gBrowser.addEventListener('load', my_load_handler, true);
    },
    tearDownBrowserUI: function(window) {
    window.gBrowser.removeEventListener('load', my_load_handler, true);
    },
    onOpenWindow: function(xulWindow) {
    let domWindow = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor)
    .getInterface(Ci.nsIDOMWindow);
    domWindow.addEventListener("load", function listener() {
    domWindow.removeEventListener("load", listener, false);
    var domDocument = domWindow.document.documentElement;
    var windowType = domDocument.getAttribute("windowtype");
    if (windowType == "navigator:browser")
    WindowListener.setupBrowserUI(domWindow);
    }, false);
    },

    onCloseWindow: function(xulWindow) { },
    onWindowTitleChange: function(xulWindow, newTitle) { }
    };
    my_load_handler定义为:
    var my_load_handler = function (evt) {
    try {
    var browserEnumerator = wm.getEnumerator("navigator:browser");
    while (browserEnumerator.hasMoreElements()) {
    var browserWin = browserEnumerator.getNext();
    var tabbrowser = browserWin.gBrowser;
    var numTabs = tabbrowser.browsers.length;
    for (var index = 0; index < numTabs; index++) {
    var currentBrowser = tabbrowser.getBrowserAtIndex(index);
    var domWindow = currentBrowser.contentWindow.wrappedJSObject;

    // identify your target page...
    if (domWindow.location.href == 'http://yourserver/yourpage') {

    // install the privileged method (if it's not already there!)
    if (!domWindow.hasOwnProperty('xdomainGET') {
    install_xdomainGET_on_my_special_page(browserWin, domWindow);
    }
    }

    }
    }
    } catch (e) {
    LOG(e);
    }
    }

    关于javascript - js的xul容器函数,我想从页面上的外部iframe调用js函数之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8225883/

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