I'm working on a chrome extension and testing it as an unpacked extension.
I managed to get to the point where I can request a quota and request a filesystem, but it won't grant any space.
From the background.js:
chrome.runtime.onMessage.addListener(
function()
{
navigator.webkitPersistentStorage.queryUsageAndQuota(
function(usedQuota, grantedQuota)
{
console.log(usedQuota + " of " + grantedQuota);
}
);
let requestedBytes = 1024 * 1024 * 10; //10mb
navigator.webkitPersistentStorage.requestQuota(
requestedBytes,
function(grantedBytes)
{
window.webkitRequestFileSystem(
PERSISTENT,
grantedBytes,
function (filesystem)
{
console.log("Name: " + filesystem.name);
console.log("Requested Size " + (requestedBytes / 1024 / 1024))
console.log("Granted Size: " + (grantedBytes / 1024 / 1024));
},
function (fileerror) {console.log("Error: " + fileerror);}
)
}
);
}
);
I have an index.html that sends a message, which activates the above code.
But this is all I can get it to output:
0 of 0
background.js:29 Name: chrome-extension_dpgmepaclcikglfooaopnbockbeenlbd_0:Persistent
background.js:30 Requested Size 10
background.js:31 Granted Size: 0
manifest.json:
{
"manifest_version": 2,
"name": "SomeRandomName",
"version": "0.1",
"browser_action":
{
},
"permissions":
[
],
"background":
{
"scripts": ["background.js"],
"persistent": false
},
"content_security_policy": "script-src 'self' 'sha256-d8KCEnecXtiRMS4Kj2vm8/UElPKEk5YsgZWRWitb0O8='; object-src 'self'" //debug
}
What's going on here?
Apparently putting "unlimitedStorage" in permissions fixes it. But I thought there was a way to do it without asking for unlimited storage.
Related
I'm having troubles getting chrome storage API to work in MV3. I've tried this so far and even in the service worker console it returns an undefined result
chrome.storage.local.set({ 'test': 'test' }, function() {
chrome.storage.local.get('test', function(result) {
console.log('Value currently is ' + result);
});
});
Here is my manifest I'm not sure if that's an issue
{
"manifest_version": 3,
"name": "NAME",
"description": "Description",
"version": "0.0.3",
"background": {
"service_worker": "background.js"
},
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
},
"action": {
"default_icon": {
"16": "favicon.ico"
},
"default_title": "Outboundly",
"default_popup": "index.html"
},
"permissions": [
"storage",
"scripting",
"activeTab",
"tabs",
"unlimitedStorage"
],
"host_permissions": ["<all_urls>"]
}
I've also tried it in the content script and within the popup context as well.
Should you call in ['test']?
chrome.storage.local.set({key: value}, function() {
console.log('Value is set to ' + value);
});
chrome.storage.local.get(['key'], function(result) {
console.log('Value currently is ' + result.key);
});
reference
I'm not sure why but I changed the position of the permissions around in the manifest, and that seemed to have fixed things. Possibly a bug.
I'm unable to enter into any of the listener functions for notifications API with manifest V2. I'm able to execute the callback function after Notification is displayed. Also I can see that the chrome.notifications.onClicked.addListener is present while using in debugging mode.
Here's my manifest.json file
{
"name": "Learning",
"manifest_version": 2,
"version": "1.3",
"description": "Notification Popup",
"background": {
"scripts": ["script.js"],
"persistent" : false
},
"permissions": [
"notifications"
],
"content_security_policy": "script-src 'self' 'unsafe-eval' https://ajax.googleapis.com/; object-src 'self'"
}
script.js
var options = {
type: "basic",
title: "Someone just replied to your mail",
message: "You have been hired in our company",
iconUrl: "stackIcon.PNG"
}
chrome.notifications.create(options,function() {
console.log('Notification Callback');
});
chrome.notifications.onClicked.addListener(function() {
console.log('notification Clicked');
});
chrome.notifications.onClosed.addListener(function() {
console.log('notification Closed');
});
chrome.notifications.onButtonClicked.addListener(function() {
console.log('notification Button Clicked');
});
chrome.runtime.onInstalled.addListener(function() {
console.log('runtime onInstalled');
})
console.log results
Notification Callback
This is an unfortunate bug in Chromium Extensions, https://crbug.com/1168477, it has been fixed in version 91 canary.
This is my first time working with javascript and chrome extensions. I wanted to append ?share=1 at the end of quora urls so i dont have to sign up. I made an attempt my looking at some answers but i am unable to find why this string is appended two times ?share=1?shared=1 at the end of my urls.
here is my code
Manifest.json
{
"manifest_version": 2,
"name": "Quora view",
"description": "View quora without signing up ",
"version": "1.0.0",
"icons": {"128": "icon128.png"},
"background": {"scripts":["background.js"]},
"permissions": [
"activeTab",
"webRequest",
"*://www.quora.com/*",
"webRequestBlocking"
]}
background.js
let host = "https://www.quora.com";
let post = "?share=1";
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {redirectUrl: host + details.url.match(/^https?:\/\/[^\/]+[\S\s]*)/)[1] + post};
},
{
urls: [
"*://www.quora.com/*",
]
},
["blocking"]
);
I'm developing chrome extension that need to send messages to Content Script and everything works until I replace default popup by separated window. After that communication on messages doesn't work.
My manifest:
{
"manifest_version": 2,
"name": "Overlight Live Reload",
"description": "Overlight Testing Tool",
"version": "0.0.1",
"content_security_policy": "script-src 'self' http://localhost:3000/; object-src 'self'",
"permissions": ["activeTab", "tabs", "storage", "declarativeContent", "<all_urls>"],
"browser_action": {
"default_title": "Overlight"
},
"background": {
"scripts": ["background/background.js"],
"persistent": false
},
"icons": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
},
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"css": [],
"js": ["contentScripts/lodash.js", "contentScripts/listeners.js"]
}
]
}
Listener (content script):
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});
Execution from extension app:
export const testSendToPage = () => {
console.log('Sending message to web page')
window.chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
window.chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
}
backgroung scipt that creates extension window:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.windows.create({
height: 400,
width:400,
url: chrome.runtime.getURL("../popup.html"),
type: "popup"
});
});
Everything works fine, when instead creating separated window I'm using default popup. What's more when I try use window.chrome.tabs.executeScript from separeted window exception I've got this.
Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-extension://ciipdholchghlckepfaecipbjleanmbg/popup.html". Extension manifest must request permission to access this host.
Messages from content script to extesion app works great. How can I communicate from extension App to content script from extension separated window?
Building a Chrome Extension for Gmail, trying to use Chrome Auth to gain access to gmail api per this StackOverflow post and the Gmail API docs, among others. I successfully receive the token with chrome.identity.getAuthToken({ 'interactive': true }, function(token) {} however when I plug the token into the request url I get the following 401 error response (code follows)
error response
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
My code:
background.js
chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
thisToken = token
chrome.runtime.onMessage.addListener(
function(request,sender,sendResponse){
var gapiRequestUrlAndToken = "https://www.googleapis.com/gmail/v1/users/mail%40gmail.com/threads?key={" + thisToken + "}"
var makeGetRequest = function (gapiRequestURL)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", gapiRequestURL, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
makeGetRequest(gapiRequestUrlAndToken);
}
);
});
}
})
manifest.json
{
"manifest_version": 2,
"key": "<key>",
"name": "exampleName",
"description": "Description",
"version": "0.0.1.7",
"default locale": "en",
"icons": { "128": "imgs/pledge_pin.png"},
"content_scripts" : [
{
"matches": ["*://mail.google.com/mail/*"],
"js": ["js/jquery.js", "js/compose.js", "bower_components/jqnotifybar/jquery.notifyBar.js"],
"css": ["css/stylesheet.css", "bower_components/jqnotifybar/css/jquery.notifyBar.css"]
}
],
"background": {
"scripts": ["scripts/background.js"]
},
"permissions": [
"identity"
],
"oauth2": {
"client_id": "<client id>",
"scopes": ["https://www.googleapis.com/auth/gmail.modify"]
}
}
I suspect it has something to do with the fact that I'm trying to use Chrome Auth for Gmail api, but other posts I've read have lead me to believe this is a viable option.
In case my code didn't give it away, I'm a newbie, so any help is most welcome, and I greatly appreciate your time.
key is for generic application secrets. For user specific tokens you need to use access_token. And token should not be wrapped in {}. If mail%40gmail.com is the actual value you are using in the URL, it won't work. It either needs to be the email address of the authenticated user or me.
So change:
"https://www.googleapis.com/gmail/v1/users/mail%40gmail.com/threads?key={" + thisToken + "}"
To this:
"https://www.googleapis.com/gmail/v1/users/me/threads?access_token=" + thisToken