选择框架

时间:2017-04-16 22:23:08

标签: javascript greasemonkey frames tampermonkey

我的目标是使用Firefox中的Greasemonkey扩展程序在a中的数组d内将值options更改为menu.html 我的问题是我试图选择/访问的元素是作为一个框架加载

我一直试图解决这个问题已有一段时间了,如果somone可以帮助我,我会很高兴。

我有两个文件main.htmlmenu.html,其内容为:

main.html(是主页)

<html>

<head>
    <meta http-equiv="Pragma" content="no-cache">

    <script language="javascript">
        document.writeln("<frameset rows='89,*,15' border='0' frameborder='0' framespacing='0'>");

        // here is the menu frame
        document.writeln("<frame src='menu.html' name='menufrm' frameborder='no' border='0' scrolling='no' target='_self' marginwidth='0' marginheight='0' noresize>");

        document.writeln("</frameset>");

    </script>

</head>

</html>

menu.html(通过“框架”加载)

<html>

<head>
    <meta http-equiv='Pragma' content='no-cache'>

    <link rel=stylesheet href='stylemain.css' type='text/css'>
    <script language='javascript' src='menuBcm.js'></script>

    <base target="_self">
</head>

<body class='mainMenuBody' topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">

    <table border="0" cellpadding="0" cellspacing="0" height="1000">

        <tr>
            <td class='menu' width="170" valign="top" align="left">

                <script language='javascript'>
                    var options = new Array('a',
                        'b',
                        'c');

                    // ultimate goal is to change the value of a to d above before 
                    // execution of the script below     

                    createBcmMenu(options); // from  menuBcm.js
                    initializeDocument();
                </script>

            </td>
        </tr>

    </table>
</body>

</html>

看起来像这样:

+----------------------------+  
| main page (192.168.1.1)    |  
|                            |  
|  +---------------------+   |  
|  | frame (192.168.1.1) |   |  
|  +---------------------+   |  
|                            |   
+----------------------------+  

Greasemonkey脚本:

// ==UserScript==
// @name        a-to-d
// @namespace   namespace
// @include     http://192.168.1.1/main.html
// @include     http://192.168.1.1/menu.html
// @version     1
// @grant       none
// @run-at      document-start
// ==/UserScript==

var newScript = `var options = new Array('d','b','c');` ;

// somehow select that element below
document.(!).innerHTML = newScript; // (!): somehow select script element in menu.html 

1 个答案:

答案 0 :(得分:2)

您可以使用window.frames.menufrm

访问框架窗口

然后做以下事情:

var frm = window.frames.menufrm;    
frm.options = ['d','b','c'];
frm.createBcmMenu(options);

无法保证再次调用该函数会运行良好但在加载该帧之前无法更改任何内容并且原始函数调用已经运行