chrome.cookies.set doesn't even run - javascript

The following code doesn't work for some reason:
Background.js
alert("1");
chrome.cookies.set({ url: "https://mywebsite.com", name: "SuperDuperTest" });
alert("2");
manifest.json
"permissions": [ "notifications", "https://mywebsite.com",
"cookies", "http://* /* ", "https://* / * " ],
Only alert("1"); ever fires. Alert 2 never does, why isn't my chrome cookies firing at all?

Where are looking to check whether your cookie is set or not?
Please use console.log() instead of alert()
Sample Code
manifest.json
Registered background page and given permissions for Cookies API.
{
"name": "Cookie API Demo",
"version": "1",
"description": "http://stackoverflow.com/questions/14448039/chrome-cookies-set-doesnt-even-run",
"permissions": [
"cookies",
"<all_urls>"
],
"background": {
"scripts": [
"background.js"
]
},
"manifest_version": 2
}
background.js
Trivial code to set cookie for cookies.html page
chrome.cookies.set({
"name": "Sample1",
"url": "http://developer.chrome.com/extensions/cookies.html",
"value": "Dummy Data"
}, function (cookie) {
console.log(JSON.stringify(cookie));
console.log(chrome.extension.lastError);
console.log(chrome.runtime.lastError);
});
Output
Go to https://developer.chrome.com/extensions/cookies.html and open developer tools as shown here, you can see cookie is being set!.
Click for Large Image
Further Debugging
If this sample code does not work, what are values of chrome.extension.lastError and chrome.runtime.lastError?

Make sure you declared the cookies permission.
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. For example:
{
"name": "My extension",
...
"permissions": [
"cookies",
"*://*.google.com"
],
...
}
source: developer.chrome.com

Related

Migrated manifestV2 to V3 after that chrome.action.onClicked.addListener not working

While migrating from manifest v2 to v3 facing issue(chrome.action.onClicked.addListener not working/invoking).
I have a manifest.json defined like this
{
"name": "dummy",
"manifest_version": 3,
"version": "5.2.0",
"version_name": "5.2.0",
"description": "The dummy v5.2.0 plugin allows our users to gain instant access to
their metadata and data.",
"action": {
"default_title": "execute.js will run (watch the Chrome DevTools' console)"
},
"content_scripts": [
{
"js": ["content.js"],
"matches": [
"https://*/*",
"http://*/*"
]
}
],
"background": {
"service_worker": "background.js"
},
"permissions": [
"contextMenus",
"tabs",
"scripting",
"storage"
],
"host_permissions": [
"https://*/*",
"http://*/*"
],
"web_accessible_resources": [{
"resources": ["*.html"],
"matches": ["https://*/*","http://*/*"]}]}
and background.js file has this code
chrome.action.onClicked.addListener(function (tab) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
setDomain({ tab: tabs[0] });
});});
I'm really lost here and it's extremely hard to debug.This code was working before migrating to manifest v3.
(I will edit this answer once the OP has given us more information)
I have created a test extension with
your manifest.json
your background.js, with setDomain({ tab: tabs[0] }); replaced with console.log("setDomain({ tab: tabs[0] });");
a content.js that only contains the code console.log("content.js");
When I click the action in my test extension, it excutes the code console.log("setDomain({ tab: tabs[0] });");
In other words: The code you have shown us is not enough to diagnose your problem.
We can only help you if you do the following:
Please upload your extension's entire source code to Github or a similar website.
If you don't want to upload your extension's entire source code:
Please create a simplified version of your extension that reproducibly demonstrates your problem.
Then upload the simplified extension's entire source code to Github or a similar website.

How to know whether turn on sync in chrome browser is enabled using javascript

I am trying to check below point for my assignment programatically
1. check if turn on sync is enabled in chrome
EDIT:
So far i have developed a chrome extension and trying to get the chrome.storage.sync object
But i am unable to get the status of the turn on sync "whether synced or no"
Below is my manifest.json
{
"name": "Check Turn on sync",
"version": "1.0",
"description": "Build an Extension!",
"permissions": ["storage"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"manifest_version": 2
}
I have provided a background.js file
is there a way in chrome.storage.sync that provides status of the sync
I am going to answer my own question, as i was able to find the answer.
I was able to get the status of sync is turned on or no along with the email ids. Here is the code :
create a manifest.json file with below content
{
"name": "Test turn on sync",
"version": "1.0",
"description": "Extension",
"permissions":[
"identity",
"identity.email"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"manifest_version": 2
}
Create a background.js file in the same folder
chrome.identity.getProfileUserInfo(function(data) {
if (data.id) {
alert(data);
}
else
{
alert("User has not turned on sync");
}
});
Add the folder as chrome extension Run the extension from chrome with the help of identity.email in permissions, you will be able to get even the email-id of the user who has turned on sync, along with status.

Chrome extension - Detect URL and execute an application

I know that with a basic chrome extension we can select domains where our extension will work (<all_urls>), but is this possible to execute an application ( internal ) when the user visit a specific domain?
I looked previously and saw "Native Messaging". ( I'm currently studying arithmetics ) I want my extension to open automaticly calc.exe when I'm on my working lab's website.
I already did this:
manifest.json
{
"name": "Mon extension",
"version": "0.0.1",
"background": {
"scripts": [ "background.js" ]
},
"browser_action": {
"default_title": "Ouvrir la calculatrice"
},
"permissions": [
"nativeMessaging"
],
"manifest_version": 2
}
But I did not understand how to manage the javascript part, can someone explain it to me ?
My calc.bat's manifest.json :
{
"name": "application",
"description": "Lauching my app",
"path": "C:\\Users\\root\\Documents\\calc.bat",
"type": "stdio",
"allowed_origins": [
"chrome-extension://blfgmcilkaooeokpomhcpnfnhppjklcb"
]
}
Calc.bat is simply: #echo off & start calc.exe
Thanks in advance. If my question is not clear enough, tell me, I'll modify it.
CouldnĀ“t get yours to work but the example I linked worked perfectly so started editing that one and ended up with this :
Download sample host
Edit native-messaging-example-host.bat and replace with :
#echo off
calc.exe
Run install_host.bat
Make a folder called app on same directory as host folder
Create 3 files : background.js, main.js, manifest.json
The content is:
background.js:
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if (message.action == "open app") {
chrome.runtime.connectNative("com.google.chrome.example.echo");
}
});
main.js:
chrome.runtime.sendMessage({action: "open app"}, function(response) {
console.log(response);
});
manifest.json:
{
// Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
"name": "Native Messaging Example",
"version": "1.0",
"manifest_version": 2,
"description": "Send a message to a native application.",
"content_scripts": [{
"js": ["main.js"],
"matches": ["<all_urls>"]
}],
"background": {
"scripts": ["background.js"]
},
"permissions": [
"nativeMessaging"
]
}
Finally, install the extension. This one in particular will open calc.exe every time you open/load a page, which can be easily changed with matches.
Another thing to note is that without the key in the manifest, it does not seem to work.

How do I block certain websites with my Chrome Extension?

I'm making a simple chrome extension for a project. I am making an extension that blocks certain URL's(social media etc) to make studying more efficient. I'm not very good in JS but I want to learn. I had some ideas that maybe it could either block the website or just draw something in a div blocking its content. Also, maybe I could input an URL into popup.html to specify the blocked website. Saving data in firebase. Also, I read that maybe its easier to use declarativeWebRequest but not quite sure how to use it.
Manifest.js
"name": "StudyBuddy",
"description": "Helps you study by blocking distracting websites",
"version": "2.0",
"permissions": [
"webRequestBlocking",
"webRequest",
"activeTab",
"tabs",
"http://*/*",
"https://*/*"
],
"content_scripts" : [{
"matches": ["<all_urls>"],
"js" : ["background.js"],
"css" : ["styles.css"]
}],
"browser_action": {
"default_title": "Blocks websites",
"default_popup": "popup.html"
},
"manifest_version": 2
background.js
console.log("Loaded extension");
function blockRequest(details) {
return {cancel: true};
}
function updateFilters(urls) {
if(chrome.webRequest.onBeforeRequest.hasListener(blockRequest))
chrome.webRequest.onBeforeRequest.removeListener(blockRequest);
chrome.webRequest.onBeforeRequest.addListener(blockRequest, {urls: ["*://*.facebook.com/*", "*://*.facebook.net/*"]}, ['blocking']);
}
At the moment my extension doesnt block anything.
Ok, your code has two issues :
Your manifest.json didn't specify background.js, so that code wasn't running.
You didn't actually call the updateFilters function from anywhere.
I corrected both those issues and this extension works fine for me, it blocks Facebook as expected.
In general I suggest you do some more reading of the documentation for extensions as you try to get started, especially the parts on background pages and event pages.
manifest.json: (note that I don't have access to your popup html/css so I had to remove that section from the manifest).
{
"name": "StudyBuddy",
"description": "Helps you study by blocking distracting websites",
"version": "2.0",
"permissions": [
"webRequestBlocking",
"webRequest",
"activeTab",
"tabs",
"http://*/*",
"https://*/*"
],
"background" : {
"scripts": [
"background.js"
]
},
"manifest_version": 2
}
background.js
console.log("Loaded extension");
function blockRequest(details) {
return {cancel: true};
}
function updateFilters(urls) {
if(chrome.webRequest.onBeforeRequest.hasListener(blockRequest))
chrome.webRequest.onBeforeRequest.removeListener(blockRequest);
chrome.webRequest.onBeforeRequest.addListener(blockRequest, {urls: ["*://*.facebook.com/*", "*://*.facebook.net/*"]}, ['blocking']);
}
updateFilters();

Chrome extension inject js

I want to create a new chrome extension but it don't work.
I want to inject a js file into web page (all web page,not only one.If i push the chrome icon on google the script must execute,if i push the icon on facebook it must execute ect.)
this is background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript({
null,{file: "backgrounds.js"} });
});
this is backgrounds.js
document.body.innerHTML="display div elem with style and id";
this is manifest.json
{
"name": "MyExt",
"description": "an extension,what else?",
"version": "1.0",
"permissions": [
"activeTab"
],
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["background.js"]
}
],
"browser_action": {
"default_title": "myExt"
},
"manifest_version": 2
}
what i wrong?
I'm on windows 8.1 Update 1 with chrome last version
Your manifest is wrong: you should set background.js as your background script:
"background" : { "scripts" : [ "background.js" ] },
and remove the "content_scripts" section.
The "activeTab" permission means that you don't need to specify host permissions to inject in the current tab upon browser action click, so no other permissions are needed.
The tabId argument is optional, you can just drop it instead of passing null. And your invocation is wrong (you're wrapping two arguments in a single object). Here's the correct way:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript({file: "backgrounds.js"});
});

Categories

Resources