Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" )
like image 303
Calimero Avatar asked Aug 14 '13 08:08

Calimero


2 Answers

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)

like image 132
Timothée Boucher Avatar answered Oct 07 '22 01:10

Timothée Boucher


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!

like image 20
Calimero Avatar answered Oct 07 '22 00:10

Calimero