Render context menu depending on selection - google-chrome-extension

I want to display a different menu option depending on whether a number or text is selected.
I've tried playing with content scripts but I can't get them to work in gmail which is where I need it to work. Here is what I have, it works on sites other than gmail (is it a https thing?)
Background.html
<script src="driver.js"></script>
content_script.js
document.addEventListener("mousedown", function(event){
if(event.button == 2) {
var selection = window.getSelection().toString();
chrome.extension.sendRequest({cmd: selection});
}
}, true);
driver.js
chrome.extension.onRequest.addListener(function(request) {
alert(request.cmd);
});
manifest.json
{
"name": "Context Menu Search",
"description": "Opens the selected text as keyword in a new window",
"version": "0.1",
"permissions": ["contextMenus"],
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"js": ["content_script.js"]
}
],
"background_page": "background.html"
}

Selection type changes context menu using chrome extension
You will have to set a listener for mouse down. There is no other way to get the selected text before the menu is created.
See this SO question:
chrome extension context menus, how to display a menu item only when there is no selection?
Here is part of the code the rest is at the link.
document.addEventListener("mousedown", function(event){
//right click
if(event.button == 2) {
if(window.getSelection().toString()) {
chrome.extension.sendRequest({cmd: "createSelectionMenu"});
} else {
chrome.extension.sendRequest({cmd: "createRegularMenu"});
}
}
}, true);

Related

Why is the Chrome extension's toolbar icon getting reset to the default icon?

In this minimal example, I have the following manifest.json:
{
"name": "Getting Started Example",
"version": "1.0",
"description": "Build an Extension!",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"permissions": ["webNavigation", "activeTab"],
"browser_action": {
"default_icon": {
"16": "red.png"
}
}
}
On every navigation, I want to change the icon in the toolbar to a green png. It works, however, there's a problem:
I'm on any page, the icon is green.
I click on a link, it triggers navigation.
The icon resets back to red for a few milliseconds
My listeners get triggered, and they set the icon back to green.
My background.js:
const setIcon = (details) => {
if (details.frameId != 0) {
return; // only update the icon for main page, not iframe/frame
}
chrome.browserAction.setIcon({
path: { '16' : 'green.png'},
tabId: details.tabId
});
}
chrome.webNavigation.onHistoryStateUpdated.addListener(setIcon);
chrome.webNavigation.onBeforeNavigate.addListener(setIcon);
chrome.webNavigation.onCommitted.addListener(setIcon);
What causes this behavior? Is there a way I can prevent the icon getting reset on navigation?

Chrome extension query: How to update context menu from content script in time for it to be included in the context menu?

I'm trying to create a chrome extension that will add DOM information to the context menu (as a child item) when an element is right clicked. The code I have so far does put the information I want into the context menu, and console log shows the child items being added at the right point (and both are free of errors), but not in time for it to be included in the initial rendered context menu.
The ultimate effect being, I have to right click the element twice to see the context menu child items created by the first click.
The (slightly simplified) code I have currently is below:
content script:
document.addEventListener("mousedown", function(event){
if(event.button == 2) {
chrome.runtime.sendMessage(event.target.nodeName);
}
}, true);
background:
var parent = chrome.contextMenus.create({"title": "Node", "contexts":["all"]});
chrome.runtime.onMessage.addListener(
function(nodename, sender, sendResponse) {
chrome.contextMenus.create(
{"title": nodename, "parentId": parent});
});
manifest.json
{
"manifest_version": 2,
"name": "My extension",
"description": "This is an example extension.",
"version": "0.1",
"permissions": ["contextMenus", "clipboardWrite"],
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*", "*://*/*"],
"js": ["contentscript.js"]
}
]
}
Is there a way I can get the information added to the context menu in time for it to be included?
Define doSomething. Then it will work.
As it is, it's creating an error in the background context (on the extension page, click on the "Inspect views: background page" to view the console for the background.

Browser action to activate only on "content_script" matches

I thought this would be a regularly asked question, but I can not find the answer for it.
Is there a way for a chrome extension to have it's browser action popup available only on content_scripts matching pages?
Eg: I make an extension for a site. I want the popup to only be available when browsing the site (on the current tab). How do I do?
For me the browser action is always enabled by default, whatever page I'm browsing, and page action never enabled.
Thanks for your help
EDIT: Here is part of the manifest, I changed with pageAction with No success. I want my icon to activate only on tabs browsing LDLC.
{
"content_scripts": [
{
"js": [ "jquery.min.js", "scrooge.js" ],
"matches": [ "*://www.ldlc.com/fiche/*", "*://ldlc.com/fiche/*" ]
}],
"manifest_version": 2,
"permissions": [ "storage", "activeTab","*://ldlc.com/*", "*://scroogealpha.esy.es/*" ],
"page_action": {
"default_title": "Worth buying?", // optional; shown in tooltip
"default_popup": "popup.html" // optional
},
"web_accessible_resources": [
],
"background":{
"scripts" : ["eventPage.js"],
"persistent": false
}
}
Use a page action and in your background script listen for tab changes. When a tab url changes, check if you want to show the page action.
In your background script do something like this:
let matches = ["://www.ldlc.com/fiche/", "://ldlc.com/fiche/" ]
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
for (let i in matches) {
if (tab.url.includes(matches[i])) {
chrome.pageAction.show(tabId)
break
}
}
})
Also add the tabs permission in your manifest.

Adding options to Chrome extension button menu? [duplicate]

I tried this : https://developer.chrome.com/extensions/options.html and made an option page.
So a selection has been added under my extension icon with the name of Option.
My question is that is there a way to rename Option and change it something like Setting or some words in other languages ?
The "Options" label at chrome://extensions is automatically adapted to the user's language. Extensions cannot change the value of this label.
The value of the "Options" option at the dropdown menu at the extension's button cannot be customized either, but you can create a new context menu item under the button as of Chrome 38. E.g.
chrome.contextMenus.create({
id: 'show-settings', // or any other name
title: 'Settings',
contexts: ['page_action', 'browser_action']
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (info.menuItemId == 'show-settings') {
chrome.tabs.create({
url: chrome.runtime.getURL('settings.html')
});
}
});
I suggest to just stick to "Options" though, because users do already know what the option does. Consistency in UI/UX is important, imagine how you productive you'd be if every application had a different way of (e.g.) closing/quiting the application.
manifest.json to test the previous script:
{
"name": "Contextmenu on browserAction button",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "Right-click to see a context menu"
},
"permissions": [
"contextMenus"
]
}
Easier way to trigger events.
chrome.contextMenus.create({
title: 'GitHub',
contexts: ['page_action'],
onclick: () => console.log('GitHub'),
});

Why does my Chrome extension's Action Icon shown on all pages in omnibox?

I have following manifest
"page_action": {
"default_icon": {
"19": "images/icon19.png",
"38": "images/icon38.png"
},
"default_title": "Helper for soiduplaan.tallinn.ee"
},
"content_scripts": [
{
"matches": [
"http://soiduplaan.tallinn.ee/*"
],
But I see icon of my app in all pages I visit:
What do I do wrong? =\
Your current code is:
chrome.tabs.onUpdated.addListener(function(a) {
chrome.pageAction.show(a);
});
This causes the page action to be shown whenever a page is loaded, ie for every tab.
If you want to restrict the page action to certain pages only, check the tab.url property:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (tab.url && tab.url.indexOf('http://soiduplaan.tallinn.ee/') === 0) {
chrome.pageAction.show(tabId);
}
});
For more info, read the docs for chrome.tabs.onUpdated.

Resources