Runtime error on chrome extension - javascript

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.

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 - 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

chrome.contextMenus is not available

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

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"

Chrome Extension: Port error: Could not establish connection. Receiving end does not exist.

When trying to communicate between my Content- and Background Script I get the following errors:
Port error: Could not establish connection. Receiving end does not exist.
Error in event handler for 'undefined': Cannot read property 'message' of undefined
TypeError: Cannot read property 'message' of undefined
background.js
function onRequest(request, sender, callbackFunction) {
console.log("Me (BS) became this Message:" + request.message);
sendResponse({message: request.message})
};
chrome.extension.onRequest.addListener(onRequest);
streamcloud.js
function contactBackground(nachricht){
chrome.extension.sendMessage({message: nachricht}, function(response) {
console.log("The Background Script got the following Message: " + response.message);
});
}
and my manifest.json
{
"name": "InstantWatch - Dev",
"manifest_version": 2,
"version": "0.7",
"permissions": ["tabs", "http://*/", "https://*/"],
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "InstantWatch",
"default_icon" : "icon.ico"
},
"content_scripts": [
{
"matches": ["http://*/*", "http://*/*"],
"js": ["jquery.js", "streamcloud.js"]
}
]
}
I found the solution to add an background_page: "background.html" with an empty background.html, but since background_page isn't supported since manifest_version: 2, I can't use that.
sendMessage and onRequest are not compatible.
If you need to support Chrome 19 and earlier, use onRequest and sendRequest:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
// Warning: Chrome 19- [receiver]
});
chrome.extension.sendRequest(message, optional_sendResponse);
For Chrome 20 - 25, use chrome.extension.onMessage and chrome.extension.sendMessage:
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
// Chrome 20+
});
chrome.extension.sendMessage(message, optional_sendResponse);
For Chrome 26+, use chrome.runtime.onMessage and chrome.runtime.sendMessage.
Note: As of Chrome 26, the deprecated methods are still supported, albeit undocumented. If you get a chance, update your extension to use the new methods, to ensure that your extension will still work in the future.
See this answer for code to create a which is compatible with Chrome 20+.
Instead of
chrome.extension.onRequest.addListener(onRequest);
Use
chrome.extension.onMessage.addListener(onRequest);
Since you are using sendMessage and not sendRequest.
Message parsing has been updated in the new version of Chrome. sendRequest and onRequest are being deprecated. It is recommended to go with sendMessage and onMessage.
Refer docs for message parsing between Content Script and Background.

Categories

Resources