0

I have something like this in my background.js:

chrome.contextMenus.create({
    title: "Log Some Text",
    contexts: ["selection"],
    onclick: info => {
      console.log(info.selectionText);
    }
  });

I'd like to have possibility to "log some text" even when it's not highlighted.

So, I replaced snippet above to:

chrome.contextMenus.create({
    title: "Log Some Text",
    contexts: ["all"],
    onclick: info => {
      // How to get DOM node here? Or text under DOM node?
    }
  });

I understand that I probably should have content script for such cases but I don't have it now and don't really want to add it for such a simple feature.

So, I'm wondering if google chrome extensions allowed to get DOM elements when you start context menu on them.

Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79
  • Maybe this can help you: https://stackoverflow.com/questions/8813051/determine-which-element-the-mouse-pointer-is-on-top-of-in-javascript – Karlen Kishmiryan Jun 08 '17 at 13:25
  • [How to retrieve the element where a contextmenu has been executed](//stackoverflow.com/q/7703697) – wOxxOm Jun 08 '17 at 13:28
  • @KarlenKishmiryan thanks, I don't know how can I use it, `info` doesn't have coordinates. But anyway thanks. – Boris Zagoruiko Jun 08 '17 at 13:29
  • @wOxxOm the answer here is for content script. I have all the stuff in background script, so the question is: should I create content script or maybe there's a workaround / hack. – Boris Zagoruiko Jun 08 '17 at 13:31
  • 2
    The only solution is to use a content script. This is how extensions work. See the [architecture](https://developer.chrome.com/extensions/overview#arch). You can inject a content script code from the background script though and check `document.activeElement`. – wOxxOm Jun 08 '17 at 13:49
  • @wOxxOm I suppose it's true. Can you pls paste it as an answer, I'll mark it as correct. Thanks – Boris Zagoruiko Jun 08 '17 at 14:37

0 Answers0