我正在尝试以编程方式点击google calendar page上的某个日期范围按钮(日,周,月,...)。我以编程方式触发按钮回调的尝试都失败了。值得注意的是,按钮的html结构实际上是一个带有附加事件监听器和几个子div的div(我在下面发布)。
我尝试过:
=
我还能做些什么来触发按钮的回调吗?有没有其他方法可以模仿用户按下它?为简单起见,我们假设我的脚本如下所示:
From: Example Account <example1@example.com>
To: <example2@example.com>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Subject: This is a test!
Date: Tue, 18 Oct 2016 10:46:57 -GMT-07:00
Here is a long test message that will probably cause some words to wrap in =
strange places.
该按钮具有以下html结构:
var month = document.getElementsByClassName("goog-inline-block goog-imageless-button goog-imageless-button-collapse-left goog-imageless-button-collapse-right")[1];
// (1)
month.click();
// (2)
var click = new Event("click", {"bubbles":true, "cancelable":false});
month.dispatchEvent(click);
// (3)
var over = new Event("mouseover", {"bubbles":true, "cancelable":false});
var down = new Event("mousedown", {"bubbles":true, "cancelable":false});
var up = new Event("mouseup", {"bubbles":true, "cancelable":false});
month.dispatchEvent(over);
month.dispatchEvent(down);
month.dispatchEvent(up);
// (4)
coords = month.getBoundingClientRect();
// I repeated 2 & 3, but set the event's clientX & clientY attributes
var click = new Event("click", {"bubbles":true, "cancelable":false});
click.clientX = coords.left + n // n is some offset within coords.left & coords.right
click.clientY = coords.top + m // m is some offset within coords.top & coords.bottom
month.dispatchEvent(click);
// (5)
// Tried using analogous methods in JQuery
var $month = // jquery selector producing month btn
$month.trigger("click");
// (6)
// Repeated 1-5 on the child elements