gpt4 book ai didi

javascript - 如何使用 greasemonkey 以编程方式单击 GMail 中的 "compose"按钮?

转载 作者:行者123 更新时间:2023-11-29 10:53:50 25 4
gpt4 key购买 nike

我正在为 GMail 创建一个脚本,它要求我在左侧复制各种链接,如收件箱、所有邮件、垃圾邮件和撰写。除了撰写之外,我的所有链接都在工作。我无法弄清楚单击时发生了什么。你可以在下面找到我的代码。我将不胜感激

// ==UserScript==
// @name GMC Test
// @namespace com.pbg
// @description test
// @include http*://mail.google.com*
// ==/UserScript==

//loading function
function tryAgain(tries) {
setTimeout(function() { init(tries++); }, 1000*tries);
}

//gets a node by XPath
function getNodeByXPath(expression, parent) {
var r = parent.evaluate(expression, parent, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
return ((r != null) ? r.iterateNext() : null);
}

//initialize
function init(tries) {

tries = tries || 0;
if (tries > 3) return; // give up, too many tries
// Locate the canvas_frame iframe
var f = document.getElementById("canvas_frame");
if (f == null) return tryAgain(tries);
// Locate the document
var doc = f.contentWindow.document;
if (doc == null) return tryAgain(tries);
// make sure all the links are loaded
if (getNodeByXPath("//a[contains(@href,'#inbox')]", doc) == null) return tryAgain(tries);

go();
}
function go() {

function fireEvent(xPath,event)//https://developer.mozilla.org/en/DOM/element.dispatchEvent
{
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent(event, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
var cb = getNodeByXPath(xPath, doc);
var canceled = !cb.dispatchEvent(evt);
GM_log("event canceled = " + canceled);
}
var doc = document.getElementById("canvas_frame").contentWindow.document;

//THE LINE BELOW WORKS
//setTimeout(function(){GM_log("let's click starred!");fireEvent("//a[contains(@href,'#starred')]", "click")}, 5000);

//THIS DOENS'T WORK
setTimeout(function(){GM_log("now let's click compose!");fireEvent("//div[@class='J-Zh-I J-J5-Ji L3')]", "click")}, 5000);
}

window.addEventListener('load', init, false );

最佳答案

如果您使用的是较新的 AJAX-y 样式 GMail,您只需将页面的 href 更改为撰写 url,如下所示*,然后让页面的事件处理程序像往常一样触发/处理主题标签更改:

document.location.href = "#compose";

或者,如果您使用的是基本 HTML View ,则可以使用 xPath 查找撰写邮件 anchor 并更改 document.location.href 以匹配链接指向的任何内容。

我确定可以编写更优雅/更灵活的 xPath 更改,但刚才的简短查看表明它有一个 accesskey 属性设置为 c,这可能足够独特以寻找正确的链接:

var nodesSnapshot = document.evaluate(
'//*[@accesskey="c"]',
document,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null
);

document.location.href = nodesSnapshot.snapshotItem(0).href;

*nb:两者都经过测试并在 Firebug 中工作,但由于在 AJAX-y View 中使用 iFrame 或其他因素等,在 Greasemonkey 脚本中的工作方式可能不同

关于javascript - 如何使用 greasemonkey 以编程方式单击 GMail 中的 "compose"按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4587811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com