I'm creating a google chrome extension that opens the previous page in a new tab, but with my current code, it just executes and creates a new blank tab. How can I make the history back button work?
Manifest
{
"manifest_version": 2,
"name": "Back",
"description": "Opens the Previous Page in a New Tab",
"version": "1.0",
"background": {
"scripts": ["popup.js"]
},
"browser_action": {
"default_icon": "icon.png",
"default_title": "Back"
},
"permissions": [
"tabs",
"history"
]
}
and Popup.js
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
var action = window.history.back(1);
chrome.tabs.create({ url: action });
});
Related
I am trying to send the currrent active tab information to a pop-up page from the background script but am getting "undefined" when doing so.
Background script:
//load popup.html in a pop-up window.
chrome.browserAction.onClicked.addListener(function() {
chrome.windows.create({'url': 'HTML/popup.html', 'type': 'popup'},
function(window) {
});
});
//return current active tab
function backgroundFunction () {
var tab = "";
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
tab = tabs[0].title;
});
return tab;
}
popup.js:
(function () {
var otherWindows = chrome.extension.getBackgroundPage();
console.log(otherWindows.backgroundFunction());
})();
My Manifest file:
{
"manifest_version": 2,
"name": "Application Test",
"version": "1.0",
"description": "Test Application",
"icons": {
"128": "icons/icon128.png",
"48": "icons/icon48.png",
"16": "icons/icon16.png"
},
"browser_action":{
"default_icon": "icons/icon16.png"
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs",
"<all_urls>",
"activeTab"
]
}
I suspect it is undefined because the background script tab is now "undefined" because of the pop-up window.
How can I sucessfully send the tab info for whatever URL the user was on to the popup.js file when they click the Extension icon?
Use chrome.runtime, most methods are deprecated in chrome.extension
chrome.runtime.getBackgroundPage(backgroundWindow => {
console.log(backgroundWindow.backgroundFunction());
});
I am using chrome.tabCapture.capture in order to capture a tab's audio from my background page:
chrome.tabCapture.capture({
audio: true,
video: false
}, (stream) => {
if (null == stream) {
chrome.extension.sendMessage({
type: "powerOff"
});
} else {
methods.createAudio(stream);
})
The tab is captured and uncaptured by sending requests from the popup.
I would like the extension to not run in the background while the tab is uncaptured, but when I switch the background persistence to 'false' in the manifest, the event page stops working (capturing) when I switch focus from the captured window.
The event page documentation says :
"If you're using message passing, be sure to close unused message ports. The event page will not shut down until all message ports are closed."
So I'm thinking about using:
setInterval(function () {
console.log("Tab Captured. Forcing event page to stay open..");
chrome.runtime.sendMessage("CAPTURING");
}, 1000);
This in order to force the page to stay open by sending a message to the popup every second.
Is this method safe, and what's the recommended interval value?
Manifest:
{
"background": {
"scripts": ["js/bg/bg.html" ],
"persistent": false
},
"browser_action": {
"default_icon": "style/128_off.png",
"default_popup": "popup.html",
"default_title": "__MSG_mainTitle__"
},
"default_locale": "en",
"description": "__MSG_description__",
"icons": {
"128": "style/128_on.png"
},
"manifest_version": 2,
"name": "__MSG_name__",
"offline_enabled": true,
"options_page": "options.html",
"permissions": [ "*://*/*", "tabCapture", "tabs", "storage" ],
"version": "2.3.9"
}
I am trying to create a quick Chrome extension (complete beginner) and just want to display an alert whenever the icon is clicked, so I tried the following:
manifest.json
{
"name": "Something",
"version": "1.0",
"manifest_version": 2,
"description": "Woohoo",
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts" : [{
"matches": ["<all_urls>"],
"js" : ["bgscript.js"]
}]
}
bgscript.js
chrome.browserAction.onClicked.addListener(function(tab) {
alert('icon clicked')
});
However when I click on my icon, nothing happens! Looking at the above - can anyone spot why this wouldn't work?
In order to be notified for browser-action's onClicked event, you need a background-page (or better yet event-page), not a content-script.
Change your manifest like this:
// Replace that:
"content_scripts" : [{...}]
// with this:
"background": {
"persistent": false,
"scripts": ["bgscript.js"]
}
If you want the browser-action to invoke something on a content-script, you need to communicate throught the background-page using Message Passing (e.g. Simple one-time requests).
E.g.:
manifest.json
{
"name": "Something",
"version": "1.0",
"manifest_version": 2,
"description": "Woohoo",
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"content_scripts" : [{
"matches": ["<all_urls>"],
"js" : ["content.js"]
}]
}
background.js
chrome.browserAction.onClicked.addListener(function (tab) {
/* Send a message to the active tab's content script */
chrome.tabs.sendMessage(tab.id, { action: 'saySomething' });
});
content.js
chrome.runtime.onMessage.addListener(function (msg) {
/* We received a message, let's do as instructed */
if (msg.action === 'saySomething') {
alert('something');
}
});
I have a simple chrome extension which displays a little icon in Google Chrome. On click, it loads a search page of my site, which in term redirects you to the right page.
https://chrome.google.com/webstore/detail/w3patrol-watch-over-any-w/addcgpijdjacmndaadfgcpbfinagiplm is the extension.
Now, Google is forcing me to update to manifest version 2, instead of 1. But this is breaking my working extension.
In manifest.json I have added manifest_version 2, but since then the icon does not work anymore when I click on it.
{
"background": {
"page": "background.html"
},
"browser_action": {
"default_icon": "icon19.png",
"default_title": "__MSG_default_title__"
},
"default_locale": "en",
"description": "__MSG_description__",
"icons": {
"128": "icon128.png",
"19": "icon19.png",
"48": "icon48.png"
},
"name": "__MSG_name__",
"permissions": [ "tabs", "http://*.w3patrol.com/" ],
"update_url": "http://clients2.google.com/service/update2/crx",
"version": "1.0",
"manifest_version": 2
}
This is background.html
<script type="text/javascript">
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.getSelected(null,function(tab) {
chrome.tabs.create( { url: "http://w3patrol.com/search.php?q=" +tab.url } );
});
});
</script>
What do I need to add/change to get it working with manifest version 2?
You just need to remove the script tag from your background page. Here's how background.js(instead of background.html) should look like:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.getSelected(null,function(tab) {
chrome.tabs.create( { url: "http://w3patrol.com/search.php?q=" +tab.url } );
});
});
And remove the 'page' property in background. Add 'scripts' property:
"background": {
"scripts": ["background.js"]
},
So I'm trying to make an extension that on click opens a tab and goes to a page. The only thing I can make it do so far is open a tab and give me this error:
No webpage was found for the web address:
chrome-extension://hgjkkhjinhilcehaaldcnopaefinlfif/https://www.google.com/
Here is the manifest.json:
{
"name": "New App",
"version": "0.1",
"permissions": ["tabs"],
"manifest_version": 2,
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"]
},
"icons": {
"48": "icon.png"
}
}
Here is background.js
chrome.browserAction.onClicked.addListener
(function(tab)
{chrome.tabs.create({'url': chrome.extension.getURL('https://www.google.com/')}, function(tab) {})
}
)
What I was trying to do was open a new tab and go to a website in the browser action. Here is the answer:
Manifest.json
{ "name": "Funny Pictures",
"version": "0.1",
"manifest_version": 2,
"description": "Rick Roll all your friends!",
"browser_action": {
"default_icon": "funnyface.png"
},
"icons": {
"48": "funnyface.png"
},
"background":{
"scripts": ["background.js"]
}
}
background.js
chrome.browserAction.onClicked.addListener(function(activeTab) {
var newURL = "http://www.youtube.com/watch?v=oHg5SJYRHA0";
chrome.tabs.create({ url: newURL });
});
I swear I tried this previously, but that's how it goes I guess.