Calimero
Calimero

Reputation: 1074

Chrome Extension ContextMenu for PDF

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" )

Upvotes: 5

Views: 1451

Answers (3)

Destiny
Destiny

Reputation: 506

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

Upvotes: 0

Calimero
Calimero

Reputation: 1074

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!

Upvotes: 4

Timothée Boucher
Timothée Boucher

Reputation: 1565

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)

Upvotes: 6

Related Questions