Chrome extension: context parameter in chrome.contextMenus.create is being ignored - google-chrome-extension

So I got this piece of code from my extension (I'm currently using webextension polyfill), it successfully creates the context menu and it's accessible when in foo.bar,
The issue is in the browser_action context menu, it's always there, I've specifically declared "page" as context when creating the contextMenu,:
browser.contextMenus.create({
id: "some-id",
title: "context menu message",
documentUrlPatterns: ["*://foo.bar/*"],
contexts: ["page"]
});
According to docs, I should add "browser_action" to make it appear in the browser_action context menu, however I've not included it yet always appear there, even if the current URL doesn't match documentUrlPatterns.
This only happens on chrome based browsers, it works perfectly in firefox.

Related

chrome extension, how to show a modal from context Menu option?

On clicking the option on context menu (of my chrome extension) I want to show a modal on the page, which contains a form.
How do you implement this?
What I have found implemented right now:
I can easily describe a form in contentScript.js and inject it.
Problems I have found:
Defining the elements to be injected in javascript is tedious. (isn't there a better way)
Website's CSS interferes with my injected elements
Possible solutions I have found:
Shadow-DOM so that website's CSS does not interfere with the injected element.
Example:
Pocket Chrome extension is exactly the type of action I want to build and it doesn't user shadow-DOM. How does it do?

Selecting Browser Action for Current Tab in Chrome Extension

I'm working on a chrome extension that manipulates certain cookies. Most of the manipulation takes place in the background service, but I need to update the icon and pass data to the browser action for the current tab.
I'm looking for an action similar to the AdBlock extension. AdBlock loads a small number in the bottom right of the icon for the number of ads blocked, so it varies from tab to tab.
When I perform this action from the background service, it seems to change across all browsing tabs. Can someone with experience in extensions point me in the right direction for this one?
This should get you started.
setInterval(function(){//every second
chrome.tabs.getSelected(null,function(tab) {//on the current tab,
chrome.browserAction.getBadgeText({tabId:tab.id}, function(badgeText){//get the tab's badge text
if(badgeText.length<1){
badgeText="0";//set the text if its empty
}
chrome.browserAction.setBadgeText({tabId:tab.id,text:badgeText/1+1+""});//and add one.
});
});
},1000);
make sure you don't run this in the console, because chrome will get the developer tool window id, and since no valid tab has that id, it will change every single tab's badgeText.
You just need to include the tab id when you set it, such as:
chrome.browserAction.setBadgeText({ text: "5", tabId: tab.id })

executeScript in background page fails silently only when DevTools undocked

My DevTools extension injects script into the inspected window in order to parse and receive messages from-- normally on page load, but also when DevTools is first opened.
The relevant line in the background page is as follows:
chrome.tabs.executeScript(message.tabId,{ file: 'insert.js', runAt: 'document_idle' },function(results){console.log(results);});
This line normally displays [null] in the background console upon success-- and is successful both when opening DevTools in docked state, and when a user navigates to a new URL with DevTools in any state. It's only not working when DevTools is opened in an undocked state; in that case, the call is failing silently and results is returning undefined.
Permissions are "tabs", "http:///", "https:///"; and again, it's only not working in this one very specific case.
Is this a known issue with a workaround or should I consider this a Chromium bug?
The test case of your bug report contains a typo.
he onMessage event listener in your event page expects the tab ID to be available as message.tabId:
chrome.tabs.executeScript(message.tabId,{ file: 'insert.js', runAt: 'document_idle' });
However, in the devtools page, the message is being sent as "tabID" (note: capital D).
chrome.runtime.sendMessage({type: 'newpageload',tabID: chrome.devtools.inspectedWindow.tabId});
Because message.tabId is undefined in the event page, Chrome defaults to inserting the content script in the active tab of the current window. When the developer tools is undocked, this tab is (coincidentally) identical to the inspected tab, so you did not notice the error.
When the devtools are undocked, the current window is the devtools. Since you cannot script the developer tools, nothing appears to happen. If you read the value of chrome.runtime.lastError in the callback of chrome.tabs.executeScript, you would have noticed that you're trying to insert the script in the wrong window.
After changing the "tabId" to "tabID", the test case works as expected.

Why does a browser action's default icon reapper after a custom icon was applied?

I have a strange problem with a browser action icon in Chrome. There is a default icon for browser action defined in manifest. The icon is displayed correctly. Then in a background page, under some conditions, I call:
chrome.browserAction.setIcon({path:"green_32.png", tabId:request.tabId});
This icon blinks for a moment, and then changes back to the default icon. The active tab and its id passed to setIcon remain the same during all the process.
Can someone suggest an idea why this can happen?
The reason why the icon was reset to default state every time is because I called setIcon before the tab finishes loading and obtains "complete" state.
I guess there should be some information about this in documentation on tabs or on browser actions, but I didn't find it: the default icon is actually applied - by-design - to a specific page after it finishes loading. I moved the call setIcon into tabs.onUpdated handler, and now custom icon persists.
This contradicts to my former understanding that the browser action icon is set on a per tab basis, regarless to a page loaded into the tab and its state.
#KonradDzwinel kindly provided a simple extension to test the case (look at the comments). I changed its background.js script to demonstrate this behaviour:
chrome.browserAction.onClicked.addListener(function(tab)
{
chrome.browserAction.setIcon({path: 'gfx/icon2.png', tabId: tab.id});
});
To reproduce this behaviour, on any tab press the browser action icon to get it changed. Then refresh the page. As a result the browser action icon reset back to default.
If this behaviour is explained in some documentation, please, write this in comments, and I'll update the answer. From what I have read so far, I was convinced that default icon is set for new tab at its creation time, and then any changes to it are solely under extension's control.

Trouble attaching to a javascript window with Watir

I am new to Watir, and am working on developing a testing tool for my work.
I have run into a problem that I cannot seem to solve, even after checking several sites.
The javascript window creation is below: (the window created holds a pdf in a window, so the only "buttons" are the minimize, maximize, close)
<a id="LogIn_HyperLink2" class="ms-WPTitle" onclick="javascript:var win = new Window({className: 'spread', title: 'Security Statement', top:0, left:1, width:750, height:365, url:'--redacted--/security.pdf', showEffectOptions: {duration:1.0}}); win.setConstraint(true, {left:10, right:20}); win.showCenter(); return false;" href="--redacted--/security.pdf" style="color:#6699cc; font-weight:bold;">Security Statement</a><br>
I have tried using both
puts browser.modal_dialog(:title, "Security Statement").exists?
puts browser.javascript_dialog.exists?
both have returned 'false'
What approach should I be taking to attach to this new window, or more directly: How can I close this new window?
You can see the page at this link (IE only)
If the window holds a PDF file it's a browser window, not a modal javascript popup (alert, confirm, prompt)
It's defined to start without all the normal menus etc active, but it's still a browser window. You can attach to it as described in the Watir Wiki section about new browser windows, using the url or the title since you know both of those (given the HTML you showed us).
If you are using Watir-Webdriver use it's window switching commands. Right now the watirspec for that is your best reference to the methods supported and how they work.
EDIT
Thanks for the link. While the above would be true for a new browser window, that's not what you are faced with. What you have there is all inside the browser HTML, created in the DOM on the fly with javascript. It's all standard HTML elements, easily interacted with once you know what's going on (this is actually IMHO easier to deal with than a popup or separate window)
Use the IE developer tools, after you click the link that makes that 'window' appear, click the icon in the toolbar of the dev tools to refresh the DOM in the dev tools and you will be able to see that.
The outermost container appears to be a div of class 'dialog', which is unique in the DOM at that point.
The window controls are three divs under that one, with classes 'spread_close', 'spread_minimize', 'spread_maximize'. There are three tables that hold the graphic elements for the top, sides, and bottom of the 'window' but there is ZERO actual content there, it's just a visual windowframe.
There is also an iframe that superimposes that window, which is I think were the content would be (I can't get it to load, maybe because I'm not authorized for it or something)
If you just want to close the window, try this:
browser.div(:class => 'spread_close').click
Since this is coming into existing due to a bunch of client side JS code you may need to use something like the 'when_present' method after clicking the link before you first start to interact with it. eg if all you want to do is click the link to open it, and then close it, you'd do something like this
browser.link(:text => 'Security Statement').click
browser.div(:class => 'spread_close').when_present.click

Resources