5

I'm developing a extension for the chrome browser and i want to add a specified contextmenu for pdf documents. I also add to specified contextmenus for the type "page" and "image". If i set the type to "all" then there is a contextmenu, but not specified for pdf documents.

Is it possible to add a specified contextmenu for pdf documents or should i use a the type "all" an it make switch case in the clickEventHandler?!

See more at: http://developer.chrome.com/extensions/contextMenus.html

These are the "file" types:

contexts ( optional array of enum of "all", "page", "frame", "selection", "link", "editable", "image", "video", "audio", or "launcher" )
Calimero
  • 1,054
  • 1
  • 7
  • 17

3 Answers3

5

I'm guessing that you want to add a context menu only when a PDF is shown in a tab, right? Just asking because I thought at first that you wanted to add the context menu on links to PDF files, which is indeed possible*. (as you probably know)

I couldn't find a way to do this directly, however one alternative could be to listen to chrome.tabs.onActivated and add or remove your context menu based on if the current URL matches a PDF file. One drawback is that it means asking for the tabs permission which might looks scary to users. ("This extension can access your tabs and browsing activity" or something like that)

*for the curious, you do it like this:

chrome.contextMenus.create({
    title: "Hello world",
    contexts: ["link"],
    targetUrlPatterns: ["*://*/*.pdf"]
});

(you would add the other options that interest you of course)

Timothée Boucher
  • 1,535
  • 12
  • 30
  • Thank you, i modified my function, and now the specified context menu works only for pdf documents: – Calimero Aug 15 '13 at 09:42
  • 1
    Just curious, does this still work as of today, given that the out-of-process pdf extension behaves very different? – bers Oct 01 '15 at 19:22
  • I don't understand why this doesn't work with `'documentUrlPatterns': ["*://*/*.pdf"]` – Winand Oct 16 '18 at 17:46
3

This functions works for me for pdf documents:

chrome.tabs.onActivated.addListener(function (info) {
    var tab = chrome.tabs.get(info.tabId, function (tab) {
        if (tab.url.indexOf(".pdf") > 0) {
            chrome.contextMenus.create({
                "id": "1",
                title: "Just for PDF Documents",
                contexts: ["all"],
                onclick: function (e) {
                }
            });
        } else {
            chrome.contextMenus.remove("1", null);
        }
    });
});

Maybe the line

if (tab.url.indexOf(".pdf") > 0) {

should edit with a expression!

Calimero
  • 1,054
  • 1
  • 7
  • 17
0

Current answers are not perfect:

  1. The way to remove context menus

  2. Not work well with new open pdf file or multiple windows

    let g_contextMenus = [{
        id: "test",
        title: "test"
    }];
    
    function createContextMenus() {
    
    for (var menu of g_contextMenus) {
        chrome.contextMenus.create({
            id: menu["id"],
            type: "normal",
            title: menu["title"],
            contexts: ["all"]
        });
        }
    }
    createContextMenus();
    
    function updateContextMenu(tabId) {
        chrome.tabs.get(tabId, function(tab) {
            var suffix = tab.url.slice(-4);
            var isPdf = suffix.toLowerCase() == ".pdf";
            for (var menu of g_contextMenus) {
                chrome.contextMenus.update(menu["id"], { visible: isPdf })
            }
        });
    };
    
    /**
     * Switch tab
     **/
    chrome.tabs.onActivated.addListener(function(info) {
        updateContextMenu(info.tabId);
    });
    
    /**
     * New open file
     **/
    chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
        var suffix = tab.url.slice(-4);
        if (info.status == "complete" && suffix.toLowerCase() == ".pdf") {
            updateContextMenu(tabId);
        }
    });
    
    /**
     * Multiple window/New window
     **/
    chrome.windows.onFocusChanged.addListener(function(winId) {
        chrome.tabs.query({ lastFocusedWindow: true, active: true }, function(tabs) {
            updateContextMenu(tabs[0].id);
        });
    });
    

References:

  1. How to get the currently opened tab's URL in my page action popup?
  2. Check if Item is already in the Context Menu
Destiny
  • 466
  • 5
  • 8