chrome.contextMenus is not available - javascript

I'm trying to develop some Chrome extensions, guided by the terrible Google developers guide here: https://developer.chrome.com/extensions/contextMenus.html
But I'm stuck with this error:
chrome.contextMenus is not available: You do not have permission to access this API.
Ensure that the required permission or manifest property is included in your manifest.json.
This is my manifest.json:
{
"manifest_version": 2,
"name": "Data v0.1",
"description": "This extension makes information handling easier...",
"version": "0.1",
"icons": { "16": "16x16.png" },
"permissions": [
"contextMenus"
],
"content_scripts": [
{
"matches": [ "*://*/*" ],
"js": [ "data.js" ],
"run_at": "document_start"
}
],
"browser_action": {
"default_icon": "16x16.png"
},
"options_page": "options.html"
}
What I'm doing wrong?

Old question, but: this API cannot be called from a content script.
If you add a background script, you'll be able to handle chrome.contextMenus

Related

Getting error while unpacking my chrome extension

I was trying my hands on local chrome extension and while unpacking that extension i got an error
Error at key 'content_scripts'. Parsing array failed at index 0: 'js': expected list, got string Could not load manifest.
following is my "manifest.json" code:
{
"name": "greeting",
"manifest_version": 3,
"version": "1.0.1",
"action": {
"default_title":"greeting",
"default_popup": "popup.html"
},
"content_scripts":[{
"matches": ["<all_urls>"],
"js":"./content.js"
}],
"permissions":["tabs"]
}
I have an understanding that this error is related to "content_scripts" array.
Use an array in js:
"content_scripts":[{
"matches": ["<all_urls>"],
"js": ["./content.js"]
}],

Chrome extension: Error handling response: TypeError: Cannot read property 'url' of undefined

I am trying to get the url of the current tab. However, I keep getting the folling error
Error handling response: TypeError: Cannot read property 'url' of undefined
at chrome-extension://haencpbflmfmghcblmadiohldhpidihf/background.js:3:25
manifest.json
{
"name": "x",
"version": "1.0",
"description": "y!",
"permissions": [
"tabs"
],
"background": {
"scripts": ["background.js"]
},
"manifest_version": 2}
background.js
console.log("h")
chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
console.log(tabs[0].url);
});
I have tried replacing tabs permission with the activeTab but I keep getting the same error.
Chrome.tabs.query is meant for v3 of manifest. You have to use chrome.tabs.getSelected while using v2.
For manifest v3, see: https://developer.chrome.com/docs/extensions/reference/tabs/#method-query

Runtime error on chrome extension

Premise:
Trying to write an incredibly simple chrome extension, and as a test, I wanted to add console logging for debugging. But, I keep getting this error
Unchecked runtime.lastError while running webRequestInternal.addEventListener: You need to request host permissions in the manifest file in order to be notified about requests from the webRequest API.
Attempted:
I have tried adding every permission I can find without any luck. Could someone please help me out!
Manifest File:
{
"manifest_version": 2,
"name": "test",
"description": "testing app",
"version": "1.0",
"background": {
"scripts": ["small.js"],
"persistent": true
},
"permissions": ["webRequest", "webRequestBlocking", "tabs", "background", "storage"],
"optional_permissions": ["http://*/*", "https://*/*", "<all_urls>"]
}
small.js
chrome.webRequest.onBeforeRequest.addListener(function(details) {
if (details.method === "POST") {
alert('here');
console.log('logging here');
} else if (details.method === "GET") {
alert('there');
console.log('logging there');
}
}, {
urls: ["<all_urls>"]
}, ["blocking", "requestBody"]);
I was facing the same issue, with similar error message. A simple update in the manifest.json fixed the problem.
The permissions array looks like this:
"permissions": [
"alarms",
"contextMenus",
"storage",
"notifications",
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
Adding the <all_urls> in permissions will fix your issue.

Chrome Extension - node.js - Socket.io

I'm trying make a chrome extension which shows a notification when I get a message from socket.io node.js server.
Can I firstly say I have looked around a lot; some people have had similar errors but I can't seem to understand the answers and how to correct them on my PC because mine may have a different layout. Talking individually like this is better in my opinion.
This is what I have in my directory extension entitled "Projects":
My extension directory
In content.js : Uncaught ReferenceError: io is not defined
var socket = io.connect('http://localhost:1337');
socket.on("hello",function(data){
console.log(data.text);
chrome.runtime.sendMessage({msg:"socket",text:data.text},function(response){});
});
In manifest.json :
{
"background": {
"scripts": [ "background.js" , "socket.io.js"]
},
"content_scripts": [ {
"js": [ "content.js" ],
"matches": ["http://*/*", "https://*/*"]
} ],
"manifest_version": 2,
"version":"1",
"name": "MyExtension"
}
In background.js :
chrome.runtime.onMessage.addListener(
function(request,sender,senderResponse){
if(request.msg==="socket"){
console.log("receive from socket server: "+request.text);
}
}
);
And finally, app.js, my server :
var app = require('http').createServer(handler).listen(1337);
var io = require('socket.io').listen(app);
function handler(req,res){
console.log(req.url);
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('Hello Node\n You are really really awesome!');
}
io.sockets.on('connection',function(socket){
socket.emit('hello',{text:"node!"});
});
This is what is inside node_modules. NOTE : THE ONLY NPM I HAVE INSTALLED IS SOCKET.IO
Inside node_modules

Cannot read property socket of Undefined TypeError in chrome.sockets API

I'm programming a chrome JS extensions that uses sockets to communicate with a Java server.
When I use the following line :
var socket = chrome.socket || chrome.experimental.socket;
socket.create('tcp',{},function(createInfo) {
I get the
Error in event handler for 'tabs.onUpdated': Cannot read property 'socket' of undefined TypeError: Cannot read property 'socket' of undefined
error (the JS code is in a tabs.onUpdated function).
My manifest file is :
{
"manifest_version": 2,
"name": "MitM Phishing Detector",
"description": "This extension protects your browser against phishing attacks based on MitM attacks",
"version": "1.0",
"background": {
"scripts": ["notify.js"],
"persistent": false
},
"browser_action": {
"default_icon": "bluetooth_device.png",
"default_popup": "choose_device.html"
},
"permissions": [
"tabs",
"http://*/*",
"background",
{"socket":
[ "tcp-connect:127.0.0.1:8081" ]
},
"notifications"
]
}
If you are using the canary/experimental build of Chrome you should be able to do
chrome.experimental.socket.create('tcp', ...)
to open a TCP socket. But just calling socket.create won't work.
Remember that this will probably only ever work on chrome... WebSockets are a much more portable solution.
To set socket properly I think that this should work (havn't tested it):
var socket = chrome.socket;
if (unfefined !== chrome.experimental.socket)
socket = chrome.experimental.socket;
if (undefined === chrome.socket)
throw "Socket unavailable"

Categories

Resources