javascript - 选择框架中的元素

标签 javascript greasemonkey frames tampermonkey

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

我已经尝试解决这个问题有一段时间了,如果有人可以帮助我,我会非常高兴。

<小时/>

我有两个文件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) |   |  
|  +---------------------+   |  
|                            |   
+----------------------------+  

油猴脚本:

// ==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 

最佳答案

您可以使用window.frames.menufrm访问框架窗口

然后做如下事情:

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

不保证再次调用该函数会正常工作,但在加载该帧并且原始函数调用已经运行之前您无法更改任何内容

关于javascript - 选择框架中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43442788/

相关文章:

javascript - 如何获取Chrome状态栏文本

php - 如何将 javascript 数据发送到 PHP?

javascript - 为什么禁止顶部窗口访问其中的框架? (除非来自同一服务器的内容)

HTML5框架替代

jquery - 通过浏览器扩展添加表值

ffmpeg分割成帧

javascript - 为什么我的状态没有在找到匹配时更新?

javascript - getJSON 执行一个帖子

javascript - document.getElementById().style.display = 'block' ;不工作

javascript - 停止执行 Javascript 函数(客户端)或对其进行调整