I have set up that when I click extn icon - my extn index.html opens.
If I click the icon a second time - I don't want it to open a second tab; I want it to focus the already open tab (if it's open).
The problem is with if it's open part. Ideally, I would not want to ask for a tabs permission, as it is very intrusive. Because of this restriction I cannot iterate all the open tabs and check their urls.
I tried storing extn tab id in chrome.local.storage and checking for it when I click the extn button - but always maintaining up-to-date tab number proved to be rather difficult. The ultimate obstacle was that when Chrome is closed - it doesnt fire any type of onClose event; thus I cannot clear local storage of now obsolete extn tab id.
So, when I reopen Chrome, click extn button - it will not open the new tab because it can see the old tab id in the storage and thinks that a tab is already open.
Is there any way to achieve the result w\o the tabs permission?
For embedded options shown in chrome://extensions UI you can simply use chrome.runtime.openOptionsPage() which will take care of refocusing the existing tab.
To find a standalone tab of your own extension use chrome.extension.getViews() and chrome.tabs.getCurrent() invoked on the other tab's JS window to get its own "current" tab:
function getOwnTabs() {
return Promise.all(
chrome.extension.getViews({type: 'tab'})
.map(view =>
new Promise(resolve =>
view.chrome.tabs.getCurrent(tab =>
resolve(Object.assign(tab, {url: view.location.href}))))));
}
async function openOptions(url) {
const ownTabs = await getOwnTabs();
const tab = ownTabs.find(tab => tab.url.includes(url));
if (tab) {
chrome.tabs.update(tab.id, {active: true});
} else {
chrome.tabs.create({url});
}
}
Usage:
openOptions('index.html')
Related
I am trying to get the url of the window that is opened by clicking the element with id 'wrpr_1431108056877h'. The new window opens but getTitle() still returns the title of the original tab. On the video provided by Browserstack I am able to verify that the current tab is indeed the one that I am trying to access.
browser
.click('#wrpr_1431108056877h')
.pause(5000)
.getTitle()
.then((url: any) => {
console.log(url);
})
.end()
.catch((err: any) => {
console.log(err.message);
done(Error('Could not move'));
});
browser.getTabIds();
should give you a list of all the window handles that webdriver knows about.
then you can use
browser.getCurrentTabId();
to figure out which window is currently in focus (Maybe this is what you said you are doing from the BrowserStack video?)
My guess is that the newly opened window isn't the currently focused window. Hence any commands executed (like getTitle()) will happen on the previous window.
You can switch the focused window to the newly opened window by calling
browser.switchTab(windowHandle)
Then you can call getTitle() and it should return the correct title.
See the web driver docs on Window here: http://webdriver.io/api/window/getTabIds.html
(scroll all the down on the left hand side to see the complete Window section)
for more info.
Please call
browser.navigate().refresh()
Before calling
browser.gettitle().
I have three different Hyperlinks on a web page
Planning.
Solutions.
Contact Us.
I want to open them in separate browser tab one by one using codedUI.
i have written the above code to obtain the list of Hyperlink
HtmlControl Hyperlink = new HtmlControl(browser);
Hyperlink.SearchProperties.Add(HtmlControl.PropertyNames.ControlType,"Hyperlink");
UITestControlCollection controls = Hyperlink.FindMatchingControls();
foreach(UITestControl control in controls)
{
if (control is HtmlHyperlink)
{
HtmlHyperlink link = (HtmlHyperlink)control;
if(link.InnerText=="Planning"|| link.InnerText== "Solutions")
{
//separate Tab logic goes here
}
}
}
I need the help related to opening a hyperlink in new browser tab. Is it possible in CodedUI ?
By default if you click the mouse middle button (or click the scroll wheel), it opens a link in new tab. I would modify your code as below in this case,
if(link.InnerText=="Planning"|| link.InnerText== "Solutions")
{
//Open Link in New tab, by clicking middle button
Mouse.Click(link, MouseButtons.Middle);
}
You can do this a couple different ways. I would use #Prageeth-Saravan 's approach first to see if it works because it's easier and actually tests your UI. You could also:
Get the URL from the found link control
Send the "New tab" keyboard shortcut
Reinstantiate your browser window object to be sure it's pointing to the new tab
Navigate to that URL
The reason why I bolded step 3 is regardless of approach, if you intend to assert or interact with anything in a new tab you're going to have to remember that the CodedUI software will still be "Looking" at the old tab until you reinitialize it.
When I click the extension icon, a popup is shown.
After that, when I try to click "URL restrictions", it will open a window, after that when I click the popup again, the popup is overlapping that url restriction window.
The above issue happens on Windows only, not on Linux.
So I have added window.open('','_self').close(); which apparently fixed the issue. But not exactly. Is it correct? I have referred this Link and Link2 but can not understand the meaning.
What is the purpose of window.open('','_self').close();?
EDIT: this is my popup.js
function click(e) {
var windowObj = window.open(site_exception_url, 'url_window', params);
windowObj.focus();
window.close();
window.open('','_self').close();
return false;
}
On Windows the popup isn't closed automatically after a new window is opened from a link within the popup.
Close it in the click handler manually, this won't hurt Linux but will help on Windows:
document.addEventListener("click", function(event) {
if (event.target.localName == "a") {
close();
}
});
The related questions linked in your question don't apply here as the first is for userscripts, not extensions, and the second isn't for popups shown by the browser when you click the toolbar button.
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 })
background.html:
// Called when the url of a tab changes.
function checkForValidUrl(tabId, changeInfo, tab) {
if(changeInfo.status === "loading") {
if (tab.url.indexOf('google.com') > -1) {
// ... show the page action.
chrome.pageAction.show(tabId);
chrome.pageAction.onClicked.addListener(function(tab){
chrome.tabs.create({url: "facebook.com", "active":true});
});
}
}
};
// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);
So, I go on google, click page action, in new tab opens facebook. I make new tab with google again, click page action and it opens 2 facebook-tabs. Its strange, because I need - 1 click on page action - 1 new tab. How to fix it?
Thanks.
The problem is that you're adding an event listener every time your tab changes.
Since you're calling checkForValidUrl every time a tab changes, chrome.pageAction.onClicked.addListener is also called every time a tab changes. Now your pageAction has two event listeners doing the same thing. You can verify this by changing tabs multiple times and see that it will open as many Facebook tabs as you have changed tabs.
To fix this, of course, you should remove the following from checkForValidUrl:
chrome.pageAction.onClicked.addListener(function(tab){
chrome.tabs.create({url: "facebook.com", "active":true});
});
and put it outside, for example, after you set up your listener for chrome.tabs.onUpdated.