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

Mic*_*orn 4 javascript google-chrome-extension gmail-api gmail.js

我只是在玩基于本教程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)

  });

});
    //
Run Code Online (Sandbox Code Playgroud)

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

提前致谢 :)

sfa*_*ota 5

不幸的是,在 2018 年引入“新 Gmail”save_draft之后,观察者似乎不再触发了。我自己加载了您的扩展程序并确认,虽然该事件仍按预期触发,但并非如此。Gmail.js 的开发人员在最近的问题报告确认,另一个事件 ,与当前版本的 Gmail 存在相同的问题。所以看起来脚本不像以前那样工作了。view_emailsave_draftsend_email

如果你仍然想让这个工作,几个选项是:

  • 找到一个替代的 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 });
});
Run Code Online (Sandbox Code Playgroud)

如果遇到任何性能问题,您可以尝试在较低级别的标签中进行搜索,而不是document.body在检查该框是否存在时进行搜索。如果您希望进一步解释其中的任何内容,请告诉我。