简体   繁体   English

通过外部程序在Firefox(或任何其他浏览器)中触发Javascript函数

[英]Trigger Javascript function in Firefox (or any other browser) from an external program

I would like to trigger a Javascript function on an already existing website I don't own by pressing a key. 我想通过按键来在我不拥有的现有网站上触发Javascript函数。

Example : trigger my_function('my parameter 1'); 示例:触发my_function('my参数1'); when I press the 1 key on the keyboard. 当我按键盘上的1键时。

I can type manually the command on the Firefox's debugger console, it works. 我可以在Firefox的调试器控制台上手动键入命令,它可以正常工作。 But I can't figure a simple way to trigger the Javascript function from an external program. 但是我找不到从外部程序触发Javascript函数的简单方法。 Can I use Firefox command line ? 我可以使用Firefox命令行吗? EventGhost ? EventGhost? AutoHotKey ? AutoHotKey? Should I write a Firefox extension ? 我应该编写Firefox扩展程序吗?

If your solution implies the use of another web browser you're welcome ! 如果您的解决方案意味着要使用其他Web浏览器,欢迎您!

I suggest you to look at this https://api.jquery.com/keydown/ . 我建议您看一下https://api.jquery.com/keydown/ Jquery could be the solution but the problem isn't clear. jQuery可能是解决方案,但问题尚不清楚。 The site you're talking about is yours or you want to inject this trigger event from outside? 您正在谈论的站点是您的站点,还是您想从外部注入此触发事件? Do you want this trigger to be injected every time? 您是否希望每次都触发该触发器?

If it's just a one-night stand you can use the browser console but if it's not you could be interested in developing an add-on or a greasemonkey extention. 如果只是一个晚上的看台,则可以使用浏览器控制台,但是如果不是,则可能会对开发附加组件或油脂猴子扩展感兴趣。

Update: Yo can use eventghost, specifically the window plugin http://www.eventghost.org/docs/pluginlist.html , to trigger a keypress on firefox. 更新:您可以使用eventghost(特别是窗口插件http://www.eventghost.org/docs/pluginlist.html)在firefox上触发按键。 Now you have to inject a javascript event handler in the page every time you visit it and most of the work is done. 现在,您每次访问它时都必须在页面中注入一个javascript事件处理程序,并且大部分工作已完成。

Update 2: Here's the basic add-on, ;) have fun 更新2:这是基本的附加组件,;)玩得开心

var self = require('sdk/self');
exports.main = function() {
    require('sdk/page-mod').PageMod({
        include: '*.org',
        contentScript: 'document.onkeypress = function (e) {console.log(e.which);}'
    });
}

To use this thing download the cfx-tools from the mozilla site (sorry I can't post more than 2 links) follow the first tutorial ( be sure to read how to use the cfx tools) and when you're ready use the code I posted above. 要使用此功能,请从mozilla网站下载cfx-tools(对不起,我不能发布2个以上的链接),请按照第一个教程进行操作(一定要阅读如何使用cfx工具),并在准备好使用代码时我在上面发布了。

  1. replace the "include" value with the name of the site of your interest 将“ include”值替换为您感兴趣的站点的名称
  2. Replace console.log(e.which) with your code 用您的代码替换console.log(e.which)

Last suggestion: 最后建议:

document.onkeypress = function (e) { if(e.which == 49){ /* trigger my function if I press 1*/}}

Bye 再见

OK ! 好 ! I finally created an AutoHotkey script and Chrome because : 我终于创建了AutoHotkey脚本和Chrome,因为:

  • I met focus problems with Javascript injection when Flash was full screen Flash全屏显示时,我遇到了Java语言注入的焦点问题
  • Chrome accepts javascript commands typed in Omnibox Chrome接受在Omnibox中键入的javascript命令

Here's how the script manages a channel change request : 脚本如何管理频道更改请求:

  • Press Escape to exit full screen 按Escape键退出全屏
  • Click in a blank zone of the webpage to be sure it gains focus 单击网页的空白区域以确保它获得焦点
  • Press F6 to activate Omnibox 按F6激活多功能框
  • Past Javascript code needed to change channel 过去的Java代码需要更改频道
  • Click in the flash video area 在Flash视频区域中单击
  • Press F to return full screen 按F返回全屏

I don't paste the script here because it's long and Stack Overflow code parser really don't like AutoHotkey code ! 我不在此粘贴脚本,因为脚本很长,而且Stack Overflow代码解析器确实不喜欢AutoHotkey代码! Just ask if you want it. 只是问你是否想要。

@EdoPut suggestion is way cleaner, but it didn't solved the focus problem when Flash went full screen. @EdoPut的建议更加简洁,但是当Flash进入全屏模式时,它并没有解决焦点问题。 Anyway thanks again EdoPut for your efforts. 无论如何,再次感谢EdoPut的努力。

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

相关问题 如何在 javascript 中从任何其他浏览器打开 Safari 浏览器? - How to open Safari browser from any other browser in javascript? 从任何其他组件反应js触发功能 - react js trigger function from any other component 从Firefox浏览器扩展中触发事件? - Trigger events from Firefox browser extension? JavaScript在Firefox以外的任何浏览器中都能正常工作吗? - JavaScript works fine in any browser but Firefox? 外部程序中的javascript打开链接,而不是浏览器 - javascript open link in external program instead browser 触发javascript功能的其他选项 - Other option to trigger javascript function 有没有办法触发使用php和javascript从Firefox浏览器打开IE窗口? - is there a way to trigger an IE window to open from firefox browser using php and javascript? HTML 视频仅出现在 Firefox 中,从本地服务器提供时不会出现在任何其他浏览器中 - HTML video appearing only in Firefox, not in any other browser when served from a local server 从外部文件中调用与其他名称相同的Java特定功能 - Call Javascript Specific Function from external file with same name that other 如何在不使用任何外部API的情况下使javascript函数等待其他函数完成? - How can I make javascript function to wait for other function to finish without using any external APIs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM