user71196
user71196

Reputation: 1

Chrome Extension - Validate Selected Text Before Creating Context.Menu

I've got the context.menu working correctly. I just don't want it to show whenever I select something other than an IP address. I have the code for validating a IP address just don't know how to implement this.

I know that chrome will add context.menus when you select hostnames and ip addresses and add "go 98.223.234.2xx" or "go cnn.com". How is chrome doing this?

Upvotes: 0

Views: 199

Answers (1)

chaohuang
chaohuang

Reputation: 4115

One thing you can try is

  1. Use mouseup and keypress event listeners for mouse and keyboard selection, respectively.

  2. Use window.getSelection() to get the selection, and compare it with the IP address you want.

  3. If the selection matches the IP address, you can create the menu by

    chrome.contextMenus.create({
      "title" : "menuTitle",
      "contexts" : ["selection"]
    });
    

Upvotes: 1

Related Questions