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: .html
These are the "file" types:
contexts ( optional array of enum of "all", "page", "frame", "selection", "link", "editable", "image", "video", "audio", or "launcher" )
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./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" )
Share
Improve this question
asked Aug 14, 2013 at 8:26
CalimeroCalimero
1,0741 gold badge7 silver badges17 bronze badges
3 Answers
Reset to default 6I'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)
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!
Current answers are not perfect:
The way to remove context menus
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 == "plete" && 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:
- How to get the currently opened tab's URL in my page action popup?
- Check if Item is already in the Context Menu