I created a Chrome extension. The function works fine in a single page.
Currently the problem is that how to check if the extension has been activated in each tab. So I can re-initialise the toolbar icon.
Steps to reproduce:
1. In page A, active the extension, change toolbar icon to close icon.
2. Open a new page B, the icon still keep using close icon.
I just want to make the toolbar icon reflect to each page.
I try to use tabs onUpdated, but it will affect the extension activated page.
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
chrome.browserAction.setIcon({
path: "images/logo.png"
});
});
You could call chrome.browserAction.setIcon() passing tabId key, like:
chrome.browserAction.setIcon({
tabId: my-tab-id,
path: {"images/logo.png"}
});
You could also update the icon from background to all tabs, when some condition happens, so you could do something like this:
if (someCondition) {
chrome.tabs.query({}, function(tabs) {
tabs.forEach(tab => {
chrome.browserAction.setIcon({
tabId: tab.id,
path: {"images/logo-A.png"}//ICON A
});
});
});
} else {
chrome.tabs.query({}, function(tabs) {
tabs.forEach(tab => {
chrome.browserAction.setIcon({
tabId: tab.id,
path: {"images/logo-B.png"}//ICON B
});
});
});
}
Related
I have created a chrome extension. I have used Selection and all context menus. I want to know how we can add popup error if any text is not selected from the page.
If any text selected then it should not show any error message.
Please find below code to display pop up:
chrome.contextMenus.create({
id: "context1",
title: "selected",
contexts: ["all"]
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
if (tab) {
if (info.menuItemId === "context1"){
var code = 'alert("text selected");';
chrome.tabs.executeScript(tab.id, { code: code });
}
}
});
After extensive searching I discovered Rob's answer: https://stackoverflow.com/a/11614440/7907844 that "webstore cannot be scripted by extensions".
I only want to extract the URL and pass it to the extension. Is there no way to achieve this?
Could I query all opened tabs from background script, and keep the active tab URL in a variable accessible to extension?
I ended up implementing background script that saves currently active tab URL in a variable that I access from within a popup.js because webstore doesn't allow us to run content scripts there. chrome.extension.getBackgroundPage() inside popup.js only worked when I used var currentUrl.
var currentUrl;
var activeTabId;
chrome.tabs.onUpdated.addListener(
function (tabId, changeInfo, tab) {
if (changeInfo.status === "complete") {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
let activeTab = tabs[0];
currentUrl = activeTab.url;
activeTabId = activeTab.id;
if(currentUrl.includes("chrome.google.com/webstore/detail")) {
chrome.pageAction.show(activeTab.id);
} else {
chrome.pageAction.hide(activeTab.id);
}
console.log("page loaded: ", currentUrl);
});
}
});
chrome.tabs.onActivated.addListener(function (object) {
activeTabId = object.tabId;
chrome.tabs.get(activeTabId, function(tab){
currentUrl = tab.url;
if(tab.url.includes("chrome.google.com/webstore/detail")) {
chrome.pageAction.show(object.tabId);
} else {
chrome.pageAction.hide(object.tabId);
}
console.log("loaded tab selected: ", tab.url);
})
});
I want to open a pop up just like hangout using a chrome extension. I would like to do it programmatically like when a notification is fired. How is this possible? My code bellow works only when the extension is open. I would like for it to somehow listen for push notifications from pub-nub in the background and fire when a notification is received.
var listenForIncomingCalls = {
init: function () {
var pubnub = PUBNUB.init({
subscribe_key: 'xxxxxxxxxx',
publish_key: 'xxxxxxxxxx',
ssl: true
});
pubnub.subscribe({
channel: 'test_incoming_calls',
message: function (m) {
console.log(m)
chrome.windows.create({ url: 'https://mobile.twitter.com/', type: 'panel' });
}
});
}
};
// Run our kitten generation script as soon as the document's DOM is ready.
document.addEventListener('DOMContentLoaded', function () {
listenForIncomingCalls.init();
});
I want to be able to change the icon of my extension according to what site I am currently browsing. How can I listen for changes in tab focus?
I think I've figured this one out. You need two listeners. One to sense when the tab has been changed, one to sense when it's been updated. And then they both can trigger the same function too run. Here's what would be in the background file...
function changeIcon() {
//query the information on the active tab
chrome.tabs.query({active: true}, function(tab){
//pull the url from that information
var url=tab[0].url;
//do whatever you need to do with the URL
//alert(url);
//change the icon
chrome.browserAction.setIcon({path: 'pathToIcon'});
});
}
//listen for new tab to be activated
chrome.tabs.onActivated.addListener(function(activeInfo) {
changeIcon();
});
//listen for current tab to be changed
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
changeIcon();
});
Simply register for tab update notifications in your background page:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{
if (changeInfo.status == "loading")
{
var url = tab.url;
var iconPath = ???
chrome.pageAction.setIcon({tabId: tabId, path: iconPath});
}
});
This handler will be called whenever a tab changes location. You don't need to care which tab is currently selected because you will have defined a different icon for each tab. Still, if you want to do it - http://code.google.com/chrome/extensions/tabs.html#event-onSelectionChanged is the way to go.
Take the "Terms of Service" example found on the developers page. If you click on "Ok," I want the page to redirect elsewhere. I'm modifying this code to show an image and the "Ok" button is really a delete button. I have everything working, except I'm not sure how to tie that button into an action.
function picFunction(theurl) {
var imgBox = new Facebox({
ajaxDelay: 100,
draggable: false,
title: 'Terms and conditions',
url: theurl,
submitValue: 'Delete',
submitFunction: function() {
imgBox.fade();
var confirm = new Facebox({
width: 200,
title: 'Confirm',
message: 'Are you sure you want to delete?',
submitValue: 'Yes',
cancelValue: 'No',
submitFunction: function() {
confirm.close();
imgBox.close();
},
cancelFunction: function() {
confirm.fastclose();
imgBox.unfade();
}
});
confirm.show();
}
});
imgBox.show();
}
You need to update the window location to reload the page at the given URL. So your inner submitFunction should look something like this, you don't need to worry about closing the Facebox because the page will redirect anyway.
submitFunction: function() {
window.location = 'http://www.pagetosendto.com'
}