Chrome 扩展和 gmail.js 阻止访问 google api

Chrome Extension and gmail.js blocking access to google api

我只是在玩弄一个基于本教程 https://cloud.google.com/blog/products/gcp/deepbreath-preventing-angry-emails-with-machine-learning 的 chrome 扩展。

当我出于某种原因使用扩展程序时,我无法使用 gmail.js https://github.com/josteink/gmailjs-node-boilerplate 获取电子邮件草稿的正文。

使用 gmail.js 样板,

    "use strict";

console.log("Extension loading...");
const jQuery = require("jquery");
const $ = jQuery;
const GmailFactory = require("gmail-js");
const gmail = new GmailFactory.Gmail($);
window.gmail = gmail;




/*
This code uses the Gmail library to observe emails being sent, turn them into XML,
then return a json data, body, which is put into the request and parsed with ML.
*/

gmail.observe.on("load", () => {
  const userEmail = gmail.get.user_email();
  console.log("Hello, " + userEmail + ". This is your Rhea talking with a new version better loading!!");

  gmail.observe.on("view_email", (domEmail) => {
    console.log("Looking at email:", domEmail);
    const emailData = gmail.new.get.email_data(domEmail);
    console.log("Email data:", emailData);
  });


//var main = function () {

  gmail.observe.on('save_draft', function (id, url, body, xhr) {

    var draftId = url['permmsgid'];

    console.log("draft saved" + draftId)

  });

});
    //

前两个控制台日志工作正常,因此不确定处理此问题的最佳方法。

提前致谢:)

不幸的是,在 "New Gmail" was introduced in 2018. I loaded your extension myself and confirmed that, while the view_email event is still firing as expected, save_draft is not. Gmail.js's developer confirmed in a recent issue report 另一个事件 send_email 与当前版本的 Gmail 存在相同问题后,save_draft 观察者似乎不再触发。所以看起来脚本不像以前那样工作了。

如果您仍然想让它正常工作,有几个选项:

  • 找到另一个 Gmail.js 事件来代替 save_draft 仍然有效
  • 使用其他一些事件处理程序获取 div.editable 的内容,例如 JavaScript 的标准 onkeypress
  • 卷起袖子,使用 Gmail.js 插件调试问题,如果可以找到修复程序,则向其开发人员发送拉取请求

编辑: 这是一个使用标准 JS 事件 keydownclick 以及等待字段出现的突变观察者的工作解决方案:

"use strict";

console.log("Extension loading...");
const jQuery = require("jquery");
const $ = jQuery;
const GmailFactory = require("gmail-js");
const gmail = new GmailFactory.Gmail($);
window.gmail = gmail;

function logInnerText(e) {
  console.log("New Email Body Text is: " + e.currentTarget.innerText);
}

gmail.observe.on("load", () => {
  const userEmail = gmail.get.user_email();
  console.log("Hello, " + userEmail + ". This is your extension talking!");

  gmail.observe.on("view_email", domEmail => {
    console.log("Looking at email:", domEmail);
    const emailData = gmail.new.get.email_data(domEmail);
    console.log("Email data:", emailData);
  });

  var observer = new MutationObserver(function(mutations) {
    let messageBodyField = document.querySelector("div.editable");
    if (
      messageBodyField &&
      !messageBodyField.getAttribute("data-found-by-my-extension")
    ) {
      messageBodyField.setAttribute("data-found-by-my-extension", true);
      messageBodyField.addEventListener("keydown", logInnerText);
      messageBodyField.addEventListener("click", logInnerText);
    }
  });

  observer.observe(document.body, { childList: true, subtree: true });
});

如果您 运行 遇到任何性能问题,您可以在检查框是否存在时尝试在比 document.body 更低级别的标签中搜索。如果您想进一步解释其中的任何一个,请告诉我。