如何使用javascript触发Google翻译页面上的监听按钮? [英] How to trigger the listen button on the Google-translate page using javascript?

查看:186
本文介绍了如何使用javascript触发Google翻译页面上的监听按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向Google翻译页面添加快捷方式.
Alt c Esc 清除textarea;按 Alt j 发音.

这是当前的用户脚本: userscripts.org/scripts/review/110928

我不知道如何触发该页面上的监听按钮.

我尝试过:

  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
    0, 0, 0, 0, 0, false, false, false, false, 0, null);
  var cb = document.getElementById("gt-src-listen"); 
  cb.dispatchEvent(evt);

但是它不起作用. (gt-src-listen是监听按钮的ID.)

解决方案

Google使用了一些漂亮的代码,似乎需要一些事件才能使该按钮发挥作用.

这在Chrome浏览器中有效:

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent('MouseEvents'); 
    clickEvent.initEvent (eventType, true, true); 
    node.dispatchEvent (clickEvent);
}

var srcListenButton  = document.getElementById('gt-src-listen');

triggerMouseEvent (srcListenButton, 'mouseover');
triggerMouseEvent (srcListenButton, 'mousedown');
triggerMouseEvent (srcListenButton, 'mouseup');

(暂时; Google不断破坏事情).

在Firefox中没有任何功能,并且即使在该浏览器中手动单击,这些按钮也从未为我使用过.

I want to add shortcuts to the Google-translate page.
Press Altc or Esc to clear the textarea; press Altj to pronounce.

This is the current user script: userscripts.org/scripts/review/110928

I do not know how to trigger the listen button on that page.

I tried:

  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
    0, 0, 0, 0, 0, false, false, false, false, 0, null);
  var cb = document.getElementById("gt-src-listen"); 
  cb.dispatchEvent(evt);

but it does not work. (gt-src-listen is id of the listen button.)

解决方案

Google uses some pretty screwy code, several events seem to be required to get that button to play.

This works in Chrome:

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent('MouseEvents'); 
    clickEvent.initEvent (eventType, true, true); 
    node.dispatchEvent (clickEvent);
}

var srcListenButton  = document.getElementById('gt-src-listen');

triggerMouseEvent (srcListenButton, 'mouseover');
triggerMouseEvent (srcListenButton, 'mousedown');
triggerMouseEvent (srcListenButton, 'mouseup');

(For now; Google continually breaks things).

Nothing works in Firefox, and those buttons have never played for me, even manually clicking, in that browser.

这篇关于如何使用javascript触发Google翻译页面上的监听按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆