简体   繁体   English

Firefox扩展:从网页js调用功能

[英]Firefox extension: invoking function from webpage js

I'm trying to automate some boring, duplicated data entry into a web application at work. 我正在尝试在工作中自动化一些无聊的重复数据输入到Web应用程序中。 I'm sorry if the answer is well known but I've spent hours in google trying to figure it out. 很抱歉,答案是众所周知的,但是我已经在Google上花费了数小时试图找出答案。 The problem is, in the web-based application none of the links are simple HTML, 'a href' type links, they use javascript. 问题是,在基于Web的应用程序中,所有链接都不是简单的HTML,“ a href”类型的链接,而是使用javascript。

I can see in one of the .js files for the website that the function invoked when a link is clicked is defined as follows 我可以在网站的.js文件之一中看到单击链接时调用的函数的定义如下

function sendEvent(eventtype, targetid, value)

...and with the Firefox debugger I can see that in order to do the simple page navigation I want to do, my extension has to invoke the websites js function as follows (the 'value' parm can be null) ...并且使用Firefox调试器,我可以看到,为了执行我想做的简单页面导航,我的扩展程序必须按以下方式调用网站js函数(“ value”参数可以为null)

 sendEvent("click", "mx709")

I found this similar question. 我发现类似的问题。 The suggested method is 建议的方法是

content.wrappedJSObject.funcFromPage()

or 要么

getBrowser().contentWindow.wrappedJSObject.funcFromPage();

... but running either of those lines in my extension doesn't seem to invoke the function and hence "click" the link I want clicked ...但是在我的扩展程序中运行这些行中的任何一行似乎都不会调用该函数,因此“单击”了我想要单击的链接

EDIT 1 - in case it wasn't clear, the code I actually put into my extension was: 编辑1-万一不清楚,我实际放入扩展中的代码是:

content.wrappedJSObject.sendEvent("click", "mx709[R:3]");

EDIT 2 - Also tried this, no dice. 编辑2-也尝试过,没有骰子。 I have the Firefox debugger open, and a breakpoint on the top of the 'sendEvent()' function. 我打开了Firefox调试器,并在“ sendEvent()”函数顶部设置了一个断点。 Every time I click a link in this web app, I hit the breakpoint, when I try lines like the above (or the following), the breakpoint is not tripped 每次单击此Web应用程序中的链接时,都会遇到断点,当我尝试上述(或以下)行时,断点不会跳闸

window.content.document.defaultView.wrappedJSObject.sendEvent("click", "mx709[R:3]");

The wrappedJSObject method should work. wrappedJSObject方法应该可以工作。

Open up scratchpad and copy paste this: 打开暂存器并复制粘贴此内容:

var indexOfTabWithMyDocument = 1; //1 means 2nd tab, 0 is first tab
Services.wm.getMostRecentWindow('navigator:browser').gBrowser.tabContainer.childNodes[indexOfTabWithMyDocument].contentWindow.wrappedJSObejct.FUNCTIONTORUN()

Set the environment of scratchpad to browser. 将暂存器的环境设置为浏览器。 And run. 快跑 It will work. 它会工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM