【问题标题】:Select element in a frame选择框架中的元素
【发布时间】:2017-04-16 22:23:07
【问题描述】:

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

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


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

Wich 看起来像这样:

+----------------------------+  
| 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 

【问题讨论】:

  • 框架已被弃用多年......为什么首先需要框架?
  • 感谢您的评论。我不是该原始代码的作者,也没有能力更改代码以删除框架。

标签: javascript greasemonkey frames tampermonkey


【解决方案1】:

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

然后执行以下操作:

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

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

【讨论】:

  • 感谢您的意见!但是通过运行上面的代码我得到ReferenceError: options is not defined
猜你喜欢
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
  • 2012-09-25
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
相关资源
最近更新 更多