0

I'm trying to set different actions for a Chrome extension based on the type of selection. Maybe I'm reading the documentation wrong, but I can't figure out what isn't working here.

manifest.json

{
   "background": {
      "scripts": [ "background2.js" ]
   },

   "description": "Creates a context menu option which copies the selected address into a new Gmail compose window.",

   "manifest_version": 2,
   "name": "New Gmail Window",
   "permissions": [ "tabs", "contextMenus" ],
   "version": "0.1"
}

background.js

chrome.contextMenus.create({
            title: "Send a New Email",
            contexts:["link", "selection"],
            onclick: checkType,
        });

   function checkType(info,tab) { 
        if (info.linkUrl.substring(0, 7) === "mailto:") {     //The "if" works as expected // 
            chrome.windows.create({
                url:"https://mail.google.com/mail/?ui=2&view=cm&fs=1&tf=1&shva=1&to=" +info.linkUrl.substr(7),
                width:640,
                height:700,
                focused:true,
                type:"popup",
            });
            console.log("The linked emails work");
        }
        else if (info.selectionText.containsNode('a',false)) { //I want to look for an <a> tag that isn't working
            console.log("this worked");
        }
        else console.log("nothing to send");
    }

Thanks for any help.

Brian
  • 4,274
  • 2
  • 27
  • 55
  • `info.selectionText` is a string. Where did you get the `containsNode` method? From this question and your previous questions, it becomes apparent that you think that it's possible to directly access the DOM of a web page from the background page. **This is not true**. If you want to change the context menu behavior based on selected text, you need to interact with the page through a content script. See this Q&A for an example on changing context menu behavior based on DOM: http://stackoverflow.com/q/14829677 – Rob W Jan 02 '14 at 23:05
  • Yea and regarding getting the DOM from a background page, its not a limitation, it just doesnt make sense since a background page handles requests from multiple pages, each with its own DOM. – Zig Mandel Jan 03 '14 at 01:45
  • @RobW You're right, I'm pretty much learning as I go. When I experimented, [I was able to do both things](https://github.com/TSCBennett/new-gmail-window/blob/master/background.js) with two separate context menu calls, and I've been trying to learn to restructure it so it recognizes what you're selecting. I appreciate the link to the other thread so I can do it the right way. – Brian Jan 03 '14 at 13:14

0 Answers0