chrome.tabs is not present in chrome object - javascript

I am adding this listener in background.js file which is a background script
chrome.tabs.onActivated.addListener( function(info) {
chrome.tabs.get(info.tabId, function(tab) {
chrome.tabs.reload();
});
});
But in the chrome object tabs are not there.
The manifest file is
{
"name": "Tab Logger",
"description": "Logs the clicked tabs with time",
"version": "0.1",
"manifest_version": 2,
"app": {
"background": {
"scripts": ["background.js"]
}
},
"permissions": [
"tabs"
],
"icons": { "16": "calculator-16.png", "128": "calculator-128.png" }
}
Can anybody tell me what am I doing wrong?

chrome.tabs API is not listed as supported for Apps, and your manifest is for an app and not an extension.
You will need to either make an extension, or not use tabs API.
To convert your manifest to an extension simply change
"app": {
"background": {
"scripts": ["background.js"]
}
},
into
"background": {
"scripts": ["background.js"]
},

Related

No output to service worker console with chrome extension

I'm trying to build an extension to monitor the xhr portion of the devtools network tab. I decided to start as simple as possible with the background script below. I'm seeing no errors on loading the extension, but don't see any output in the console.
manifest.json:
{
"manifest_version": 3,
"version": "1.0",
"name": "Hello World!",
"description": "Learning how to make a chrome extension!",
"icons": {
"16": "images/puppy16.png",
"48": "images/puppy48.png",
"128": "images/puppy128.png"
},
"action": {
"default_icon": "images/puppy.png",
"default_popup": "popup.html"
},
"background": {
"service_worker": "background.js"
},
"host_permissions": [
"https://cnn.com/*"
],
"permissions": [
"webRequest"
]
}
In my background.js:
(function () {
chrome.webRequest.onCompleted.addListener(
function (details) {
console.log('HELLO THERE!!!!', details);
},
{ urls: ["<all_urls>"] }
);
}());
What am I doing wrong?

How to execute content script using chrome extension to a url starting with "chrome-extension://....../options.html"

I have used "<all_urls>" for permissions.
But, I am getting this error. "Cannot access contents of the page. Extension manifest must request permission to access the respective host."
chrome.tabs.create({ url: 'chrome-extension://padekgcemlokbadohgkifijomclgjgif/options.html#!/profile/proxy' }, function(tab) {
chrome.tabs.executeScript(tab.id, { file: 'js/content.js' });
});
manifest.json
{
"manifest_version": 2,
"version": "1.0.0",
"name": "Gmail Automation",
"description": "Automation",
"permissions": [
"storage",
"notifications",
"<all_urls>"
],
"icons": {
"16": "resources/img/16.png",
"32": "resources/img/32.png",
"48": "resources/img/48.png",
"64": "resources/img/64.png"
},
"web_accessible_resources": ["js/content.js"],
"background": {
"persistent": false,
"scripts": [
"/js/background.js"
]
}
}
If this is not possible. Then please suggest me other ways to do. My goal is to change or modify options in other existing extensions.

document.getElementsBy is undefined

I want to create my first Firefox-Addon so I am really inexperienced.
I want to create an Addon, to modify the YouTube-Website.
background.js
function editVideo() {
document.getElementsByTagName("video")[0].playbackRate = 2;
document.getElementById("movie_player").stopVideo();
document.getElementById("movie_player").setPlaybackQuality('hd720');
document.getElementById("movie_player").playVideo();
}
browser.browserAction.onClicked.addListener(editVideo);
manifest.json
{
"description": "YouTube-Test",
"manifest_version": 2,
"name": "YouTube-Test",
"version": "1.0",
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_icon": {
"48": "icons/logo_48.png",
"64": "icons/logo_64.png"
}
}
}
So if I click the Button, the Playbackrate should be 2 and the quality 720p.
But nothing happens! If I type thoose commands directly into the console, it works.
I always get this error:
document.getElementsByTagName(...)[0] is undefined
or:
document.getElementById(...) is null
I would appreciate your help.
{
"description": "YouTube-Test",
"manifest_version": 2,
"name": "YouTube-Test",
"version": "1.0",
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["https://www.youtube.com/*"],
"js": ["runsOnWebPage.js"],
"run_at": "document_idle"
}
],
"browser_action": {
"default_icon": {
"48": "icons/logo_48.png",
"64": "icons/logo_64.png"
}
}
}
The js file within the content_scripts should now run on youtube, you might want to add more "matches" since this one only covers that specific link, and youtube has like 1000 different links

Chrome Extension: How do I get me Content Script Extension to show up on select websites

I would like my Chrome Extension to on show up on google and amazon. My manifest.json looks like this:
{
"background": {"scripts": ["background.js"]},
"content_scripts": [
{
"matches": ["*://*.google.com/*", "http://www.amazon.com/*", "*://*.amazon.com/*"],
"js": ["background.js"]
}
],
"name": "Denver Public Library Lookup",
"description": "Does Stuff",
"homepage_url": "http://www.artifacting.com",
"icons": {
"16": "icon-16.png",
"48": "icon-48.png",
"128": "icon-128.png" },
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
],
"version": "1.0",
"manifest_version": 2
}
But it doesn't show up on either google or amazon and I can't figure out why.
Here is my background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {file: "bookmarklet.js"})
});
And here is the bookmarlet.js
setTimeout('x99.focus()', 300);
var re = /([\/-]|at[at]n=)/i;
if (re.test(location.href) == true) {
var isbn = RegExp.$2;
var x99 = window.open('http://searchsite/search/searchresults.aspx?ctx=1.1033.0.0.6&type=Keyword&term=' + atatn, 'Library', 'scrollbars=1,resizable=1,top=0,left=0,location=1,width=800,height=600');
x99.focus();
}
Any ideas? Thanks for your help.
Many mistakes in the code.
You don't need content script here, all operations could be executed
within background page content
It it hard to make background page
code works within content script, this is definitely not a your case.
So using the same background.js as both background and content script
does not work in your case at least
Manifest does not declare browser
action.
And so on
I strongly suggest to start with Google extension documentation. You will save a lot of your time.
How I think files might look like
manifest.json
{
"background": {"scripts": ["background.js"]},
"name": "Denver Public Library Lookup",
"description": "Does Stuff",
"homepage_url": "http://www.artifacting.com",
"icons": {
"16": "icon-16.png",
"48": "icon-48.png",
"128": "icon-128.png" },
"browser_action": {
"default_icon": {
"19": "images/icon-19.png",
"38": "images/icon-38.png"
},
"default_title": "Do Staff" // optional; shown in tooltip
},
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
],
"version": "1.0",
"manifest_version": 2
}
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
// You need more sothisticated regexp here which checks for amazon and google domains
var re = /([\/-]|at[at]n=)/i;
if (re.test(tab.url)) {
var isbn = RegExp.$2;
var url = "http://searchsite/search/searchresults.aspx?ctx=1.1033.0.0.6&type=Keyword&term=" + isbn;
chrome.windows.create({
url : url,
left: 0,
top: 0,
width: 800,
height: 600,
focused: true,
type: "popup"
});
}
});
bookmarlet.js is not needed

Chrome Extension With Background Page Not Working With Manifest Version 2

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"]
},

Categories