Chrome app not saving data to local storage [duplicate] - google-chrome

This question already has answers here:
How to inspect contents of storage.local in Chrome Packaged App?
(2 answers)
Closed 8 years ago.
I am developing a chrome app that needs to save some data to the local storage and retrieve it afterwards.
I follow the guides on the Chrome storage API
https://developer.chrome.com/extensions/storage
and have a simple code to save data:
function saveCurrentPort()
{
chrome.storage.local.set({'last_port': connectedPortPath}, function() {
// Notify that we saved.
writeConsole('JS last port saved:' + connectedPortPath );
if (chrome.extension.lastError) {
alert('JS An error occurred: ' + chrome.extension.lastError.message);
}
});
}
After calling this with valid data, nothing gets saved in the local storage. Looking into the Resource tab in the developer tools window for my App I see no entry in the Local Storage section (nor in the Session Storage).
Of course, attempting to retrieve this data fails because the data isn't found.
This should be straight forward but simply doesn't work for me.
I run the App locally in developer mode.
Looking for other questions here I didn't see a case like this.
Any idea why it isn't working? am I missing something?
Thanks

Did you declare the "storage" permission to your manifest.json?
{
"name": "My extension",
...
"permissions": [
"storage"
],
...
}
Plus, alert() is not allowed from chrome app.
I think there must be more errors. you might need to fix it first.

Besides permission you need to put the value of 'last_port'
var last_port_var= 'last_port';
var portObj= {};
portObj[last_port_var] = connectedPortPath;
Now set the value by object:
chrome.storage.local.set(portObj, function() {
//code goes here....
}
Hope this helps.

Related

Save Gmail Attachments on Google drive - unknown file type

I looked for previous questions on this subject and found some posts from a few years ago. They don't exactly match my problem and when I try them I get more run errors than the script I am using. In my case, my script creates the files but they show up in my google drive as "unknown file" and can't be opened or previewed in Google drive. However, if I download the file to my pc, it opens and previews correctly and without any problem. How do I correct the "unknown" designation in my Google Drive?
I've tried a lot of possible solutions but either they get run time errors or I end up with an "empty" file.
Does anyone have any ideas on why this is happening or how to correct the unknown files attribute?
//pasted by Cooper as an example
function putyourcodehere() {
//this was posted via a ctrl-K
}
Save Attachment to a folder
function lfunko() {
const query = 'subject:ThisIsATestMessage';
Gmail.Users.Messages.list(gobj.globals.calendarid, { q: query }).messages.forEach(m => {
GmailApp.getMessageById(m.id).getAttachments().forEach(a => {
DriveApp.getFolderById(gobj.globals.testfolderid).createFile(a.copyBlob()).setName(a.getName());
})
})
}
gobj.globals is an object of global variables I use so that I don't have to redact them when used within answers that will be posted on SO. Just treat them as things you will have to replace with your own local variables. I used gobj.globals.calendarid because that's my email address which is what they want for the userid
Of course you will have to enable the Gmail API

Chrome Web Store In-App Payments: Errors getting SKUs

I'm trying to get the SKUs available for a freemium Chrome Extension I'm developing.
I'm following all of the documentation here:
https://developer.chrome.com/webstore/payments-iap
...and I'm using the provided buy.js file, but it doesn't seem to work and the returned error messages are useless: "INVALID_RESPONSE_ERROR"
My code:
google.payments.inapp.getSkuDetails({
parameters: {env: 'prod'},
success: (r) => {
console.log(r);
},
failure: (err) => {
console.log(err);
},
});
Thoughts:
- Am I missing some permission in my manifest? I don't see any mention that it needs any additional ones.
Other StackOverflow questions have mentioned needing to proxy due to region issues. I'm in the states, shouldn't be an issue.
I've tried the above from both an options page and a popup - does it need to happen in a background page?
I'm pretty baffled. Any help is appreciated!
Thanks.
Updates:
The above works when released (in prod), but not locally
In prod you cannot buy your own thing (heads-up). It'll give you some stupid, meaningless error, but won't tell you that.
Still can't get this to work locally which means I have to test in prod.
If you need this to work locally, you must set the 'key' in your manifest.json file. When you reload it, it will show the same ID as the loaded extension from production.
Here are instructions on how to get the relevant key
If you debugging your extension in unpacked mode, you may need to set production "key" in your manifest.

welcome page loads when Allow in incognito is checked/unchecked in Chrome

I am new to chrome extensions.I used chrome.runtime.onInstalled to load a html page whenever the extension is installed or updated.But when i am testing it in chrome, whenever i check/uncheck Allow in incognito the same html page loads each time.How to avoid this behaviour? I used "incognito":"split" in manifest.
I wish you'd posted the code so I could try to replicate the problem and give a specific solution but the easy solution is to use chrome storage API to save the extension's version when welcome.html is opened and compare it to the current version next time onInstalled is fired.
If the stored version is the same don't open it. If it's undefined or older, open it.
Get your extension's version by extracting it from chrome.extension.getURL("manifest.json")
Edit:
After a bit of googling it seems you can access the manifest more directly. Get the version number using the code below.
var version = chrome.runtime.getManifest().version;
Edit:
It seems the previous version is supplied in the callback when you update so you don't need to store anything. The object provided can be compared to the current version using chrome.runtime.getManifest().version
Something like this:
chrome.runtime.onInstalled.addListener(function (details) {
if(details.reason === "install"){
chrome.tabs.create({url: "welcome.html"});
}
else if(details.reason === "update"){
var currentVersion = chrome.runtime.getManifest().version;
var previousVersion = details.previousVersion;
if(previousVersion !== currentVersion){
chrome.tabs.create({url: "welcome.html"});
}
}
});
I don't think you can. I assume that when you uncheck "Allow in incognito", Chrome nukes the local state of the (split) incognito instance.

How can I enable the file upload permission?

I'm trying to upload images generated in my Flash application to an album on Facebook. This was working earlier in the year, but revisiting the code I now get the following OAuthException:
(#324) Requires upload file
I am using the most recent version of the ActionSccript Facebook API. The setup works like this:
First I do the authentication check with PHP to ensure users have granted permission before having to wait for the Flash to load. I'm requesting the publish_stream and user_photos permissions. The access token comes back correctly.
Once the user is authenticated the Flash is loaded and performs its own initialisation, passing fileUpload=true as part of the init object:
var initObject:Object = {
channelUrl : "myChannelURL.html",
fileUpload : true
}
Facebook.init(
'myAppID',
myCallbackFunction,
initObject,
myAccessToken
);
This seems to work as expected, the callback receives the uid of the current user.
At the end of my application I POST a Bitmap object to a predetermined album:
Facebook.api(
albumID+"/photos",
onImagePost,
{
message:"",
image:new Bitmap(myBitmapData),
fileName:''
},
URLRequestMethod.POST
);
At this point Facebook returns a 400 response:
"error": {
"message": "(#324) Requires upload file",
"type": "OAuthException"
}
What more do I need to do to ensure that this permission is being included?
It turns out that this was not a permissions error at all. Since I last deployed this code Facebook have tightened up their restrictions a bit, and the fileName parameter passed as part of the api call can no longer be an empty string. Simply passing any old text as a file name fixes the problem.
Facebook.api(
albumID+"/photos",
onImagePost,
{
message:"",
image:new Bitmap(myBitmapData),
fileName:'FILE' // required to be non-empty
},
URLRequestMethod.POST
);
Im not sure if this is a solution that can be translated into the Actionscript SDK... But, with the PHP SDK there is a method inside the facebook SDK that is called setFileUploadSupport - try looking in the code for a place to set that parameter to true.

Local Storage in HTML5

I am saving a value in the local storage (html 5) in page 1 and then when i go to page 2, the values in the localstorage are gone. Could you please let me know what could be the issue?
Editing
I am actually using the following functions to set the data on the local storage
function set(key, data)
{
localStorage.setItem(key, data);
}
function get(key)
{
return localStorage.getItem(key); }
function remove(key) {
localStorage.removeItem(key); }
function clear() {
localStorage.clear();
}
I have issues getting it to work on Firefox and dont have any issues on Chrome. This looks like an entirely different issue.
Thanks and Regards
Abishek R Srikaanth
You don't provide enough detail to troubleshoot this properly, but check these things out:
Are you previewing your design locally (e.g. via the file:// protocol), or are you on a proper web server? Only http:// requests will work accurately for you
Local storage and Web SQL storage work by reference to the originating domain or IP address (again why point #1 is important): is your second page still in scope?
What happens when you go back to page 1? Do the values re-appear?