触发Chrome扩展中的函数,当打开/更新Google日历事件时。

huangapple go评论69阅读模式
英文:

Trigger a function in a Chrome extension when a Google Calendar Event is opened / updated

问题

我开始构建一个Google日历插件,我想将其迁移到Chrome扩展(以获得更多的UI灵活性)。在Google脚本插件中,可以定义一个触发函数,每当创建/更新日历事件时都会执行(请参见这里)。

您知道我如何在Chrome扩展中实现类似的行为吗?基本上,我想定义一个函数,在用户打开/更新事件时调用它(如下图所示)。

我对前端开发非常陌生,所以任何帮助/指导都将不胜感激。

非常感谢。

触发Chrome扩展中的函数,当打开/更新Google日历事件时。

我详细研究了Google日历API文档,但未找到解决我的问题的方法。

英文:

I started building a Google Calendar Add-on, which I would like to migrate to a Chrome extension (to have more flexibility for the UI). In Google Scripts add-ons, one can define a trigger function that executes whenever a calendar event is created / updated (see here).

Do you know how I can have a similar behaviour in a Chrome extension? Basically, I would like to define a function to be called whenever the user opens / updates an event (such as in the picture below).

I am very new to front-end dev so any help / pointers would be greatly appreciated.

Many thanks

触发Chrome扩展中的函数,当打开/更新Google日历事件时。

I researched the Google Calendar API documentation extensively but couldn't find a solution to my problem.

答案1

得分: 1

我是新手开发Chrome扩展程序,所以可能会有所遗漏,但我认为你不能以这种方式定义扩展程序中的触发器。通常,你只是在“内容”和“背景”页面之间发送消息。

话虽如此,你可能可以从这个答案中找到所需的内容。我自己没有在Chrome扩展程序中尝试过它,但我在Google日历页面上的Chrome控制台中尝试了一下,当我单击我的日历以打开事件时,我立刻收到了很多通知。"创建事件"窗口被包含在一个"span"元素中,所以我是这样定义MutationObserver的:

let observer = new MutationObserver(mutations => {
    for(let mutation of mutations) {
         for(let addedNode of mutation.addedNodes) {
             if (addedNode.nodeName === "SPAN") {
                 console.log("创建日历已打开", addedNode);
              }
          }
     }
 });
 observer.observe(document, { childList: true, subtree: true });

你可能需要调整节点选择器以精确获取你想要的内容。

英文:

I'm new to developing Chrome Extensions myself, so I may be missing something, but I do not think you are able to define triggers in that way with Extension. You're usually just sending messages between the Content and Background pages.

All that being said, you might be able to get what you need from this answer. I haven't tried it within a Chrome Extension myself, but I just tried it out in the Chrome console while on the Google Calendar page and I did get a lot of notifications immediately when I clicked on my calendar to open an event. The "Create an Event" window is enclosed within a "span" element, so that's how I defined the MutationObserver:

 let observer = new MutationObserver(mutations => {
    for(let mutation of mutations) {
         for(let addedNode of mutation.addedNodes) {
             if (addedNode.nodeName === "SPAN") {
                 console.log("Create a Calendar Opened", addedNode);
              }
          }
     }
 });
 observer.observe(document, { childList: true, subtree: true });

You'll probably need to fiddle around with the node selector to get exactly what you want though.

huangapple
  • 本文由 发表于 2023年7月17日 16:25:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定