Error Trying to Get Cookies From Chrome Extension - javascript

I have a very basic Chrome extension. I can execute JS just fine but accessing anything in the Chrome API seems to be an issue. I am trying remove cookies for a particular site. However, I get the following error when my code executes.
The error:
content.js:6 Uncaught TypeError: Cannot read property 'getAll' of undefined
My Code:
(function(){
chrome.cookies.getAll({}, cookies=>{
_.forEach(cookies, cookie=>{
chrome.cookies.remove({name: cookie.name, url: "www.mydomain.com"});
});
});
)();
I figured it may have something to do with my permissions but I am not sure. Here is my manifest.json file.
{
"manifest_version": 2,
"name": "Hello World Extension",
"version": "0.1",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["jquery.js","content.js"]
}
],
"permissions": [
"cookies"
]
}

To use the cookies API, you must declare the "cookies" permission in your manifest, along with host permissions for any hosts whose cookies you want to access.
{
"manifest_version": 2,
"name": "Hello World Extension",
"version": "0.1",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["jquery.js","content.js"]
}
],
"permissions": [
"cookies",
"http://*/*",
"https://*/*"
]
}
To remove the cookies, try this:
function removeAll(url){
chrome.cookies.getAll({}, function(cookies) {
for (var i in cookies) {
chrome.cookies.remove({"url": url, "name": cookie.name});
}
});
}
I hope this works

Related

How to fix "Could not load JavaScript 'content.js' for content script" error

I'm following this video and have also enabled json formatter extension. But can anyone help me understand why the below error is produced:
this is the manifest.json file
{
"manifest_version": 2,
"name": "My Cool Extension",
"version": "0.001",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["content.js"]
}
]
}
this is the content.js file
console.log("My First Attempt");

How to get screenshot from chome.tabs.captureVisibleTab

I'm using simple js to take dataURI of an image using chrome tabs. I followed all the steps provided in the docs. But when I run and console it, it gives Cannot read property 'captureVisibleTab' of undefined error. What was the problem? Do I need to invoke any libraries? I have gone through many posts but none solved my problem.
manifest.json:
{
"manifest_version": 2,
"name": "demo",
"description": "my demo",
"version": "1.0",
"browser_action": {
"default_popup": "sample.html"
},
"permissions": [
"tabs",
"<all_urls>"
],
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": ["sample.js"],
"run_at": "document_end"
}
]
}
sample.html :
<html>
<head>
</head>
<body>
Hello...
</body>
</html>
sample.js:
window.onload = function(){
chrome.tabs.captureVisibleTab(null,{},function(dataUrl)
{
console.log(dataUrl);
});
}
Thanks!

Chrome extension Content Script not working

I built a Chrome Extension script that is supposed to run on Reddit.
My script:
console.log("hello world");
My manifest.json
{
"manifest_version": 2,
"name": "Name",
"description": "Desc",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": [
"*://reddit.com/*"
],
"js": [
"contentscript.js"
],
"run_at": "document_end"
}
],
"permissions": [
"tabs", "*://reddit.com/*", "activeTab"
]
}
The script doesn't show up in the "Content Script" section in the chrome dev tools. Does anyone have an idea why my extension is not running?
"*://reddit.com/*" doesn't match a valid url, you should use "*://*.reddit.com/*"

Web RTC screen share not working in Chrome

I am using https://simplewebrtc.com/ for webrtc everything is working fine in Firefox but when I try same in chrome its not working here is my code:-
var webrtc = new SimpleWebRTC({
localVideoEl: 'local-videos-container',
remoteVideosEl: 'videos-container',
autoRequestMedia: false,
url: '//192.168.1.51:8001'
});
$(document).on('click', '#share-screen', function () {
webrtc.shareScreen(function (e, d) {
if (e) {
alert(e); // throwing NavigatorUserMediaError
}
});
});
I have add chrome extension as well here is code of extension:-
{
"name": "Screensharing Sample",
"description": "Screensharing utility sample for getscreenmedia",
"version": "0.0.1",
"manifest_version": 2,
"minimum_chrome_version": "34",
"icons": {
},
"permissions": [
"desktopCapture"
],
"background": {
"scripts": ["background.js"]
},
"content_scripts": [ {
"js": [ "content.js" ],
"matches": ["https://192.168.1.51:2013/*"]
}],
"externally_connectable": {
"matches": [
"https://192.168.1.51:2013/*"
]
}
}
What I am doing wrong. Please help me
Thanks
Seems you are not adding matches domains correctly.try to replace matches with following code:-
replace "matches": ["https://192.168.1.51:2013/*"] with "matches": ["https://192.168.1.51:*/*"]
let me know if this worked.

chrome.webRequest event does not get triggered

I am trying to load and run a very basic extension that blocks all URLs but nothing happens.
The MANIFEST.JSON file:
{
"manifest_version": 2,
"name": "Dial2Action",
"description": "This is my description",
"version": "1.0",
"background": {"scripts":["background.js"]},
"permissions": [
"webRequest",
"webRequestBlocking",
"https://app.dial2web.com/"
]
}
and the background.js file:
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {cancel: true}; },
{urls: ["<all_urls>"]},
["blocking"]);
I will be glad to get a hint or a reference to a working simple redirect extension.
That's because you have only the block permission for "https://app.dial2web.com/".
You need the permission for all urls:
{
"manifest_version": 2,
// other stuff
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>"
]
}
This works fine for me.

Categories

Resources