Google Chrome Extension Persistence - javascript

My question is quite simple, i need to develop a Google Chrome Extension and by the way create dynamically HTML/CSS files. Basically i was thinking to do this with javascript but it's not possible for security reason. So i'm thinking about using directly Chrome API.
Is there a persistance API with chrome ?

Chrome Extensions use localStorage for data persistence. Check out http://diveintohtml5.ep.io/storage.html for a tutorial.
Note that only strings can be saved to localStorage. You'll need a JSON parser/stringifier if you want to load/save objects.

It is possible to do this with javascript. Chrome is very strict when it comes to calling scripts outside its domain(your extension folder I mean).
1. Make sure you are making js calls from your background page and not your content scripts.
2. If you are making js/ajax calls, note that chrome always sends an OPTIONS request. even for GET requests. So your server have to be able to grant permisions.
I hope these 2 points help your js. However, as everyone is saying, LocalStorage does a really good job.

I'm not completely sure of what you are asking but take a look at Local Storage
Hope it helps!

Related

How can I get make cookies work without webserver?

I have made some cookies using JavaScript. The code works but I understand that they need to be run using HTTP or some other protocol. The most obvious answer would be run the website using a webserver but that seems like a bad option for me as I am on a school network with restrictions and I need to make my website distributable so that I can send it to the exam board.
Background: the website is made-up of notepad documents, converted to html documents. So open them with browser and the webpage is displayed. But the cookies don't run properly so I need a way to make the cookies work without a webserver if possible.
I was thinking maybe there is a piece of development software that lets you view your website using protocols. It would be great if there was a piece of software that lets you compile your website into something small and distributable but with inbuilt HTTP or something. I don't know. Maybe Visual Studio has something?
You can't use cookies without HTTP, because cookies are an HTTP mechanism.
Instead of cookies, you could use localStorage, which works even with files loaded from file:// URLs (e.g., without a web server) (except on IE — quelle shock!). localStorage is also dramatically easier to use from JavaScript than cookies:
// Save a string in local storage
localStorage.setItem("foo", theString);
// Get it (perhaps on page load)
var theString = localStorage.getItem("foo");

Does chrome extension has it's own document.cookie?

I'm writing a chrome extension. I want to store some data in the browser cookie so that I can use it later. Cookie is a perfect way to do that. Does chrome extension have it's own document cookie for this like all the websites?
I got this result when I did some research https://developer.chrome.com/extensions/cookies
But it mostly talks about cookie API and getting cookies of other websites hence the question. Also does chrome storage have any advantages over using a cookie? I just need to store 2/3 key value pairs. https://developer.chrome.com/apps/storage
I will say Just one line and you will understand it.
Cookies are always related to a website/domain.
It does not make sense to ask if Chrome extension has a cookie. You can have a cookie for every domain.
Some more information to help you solve your problem. If you see chrome extension model, you can see there are
Background Scripts
Content Scripts
Popup Page/Scripts
If you want to store cookie in a background script/ popup script, then you can definitely do it. But that cookie will be saved for the domain of your background script which is essentially your chrome extension id.
If you store cookie in a content script, then you are storing information in cookie which belongs to the domain on which your content script is injected.
One one hand, yes, cookies are available in Chrome extensions. But this is a very unorthodox method of storing data in extensions.
As you correctly pointed out, chrome.cookies API is for manipulating other pages' cookies. The common way of working with cookies in JS is document.cookie.
What are the common ways to store persistent data?
Two classic ways are localStorage and chrome.storage.
I've answered about them before; see this answer for comparison between them, and this answer for a usage example.
To decide what you need, the most important question is: do you need to access data from a content script?
If no, using localStorage may be simpler.
If yes, you will need to use either chrome.storage, or message passing.

How to Access "Images" in "Resources" via javascript

I'm using google chrome.
These images are lagging the browser, so I need away to access it and clean it all.
You are asking how to solve the wrong problem. If your high-frequency ajax calls return images that they don't need, then you are making bad ajax calls. Deleting the files would be treating a symptom when you should be treating the cause.
Find a way to not download them in the first place. The result of an ajax call can be scoped, filtered, or even parsed manually. There's no reason to allow the full result of your ajax call to store these extra resources that you don't want. If you want to know specifically how to do that filtering, you'll need to post the code that generates your ajax calls.
_generated_background_page.html is a file created by an extension installed into Google Chrome. If you are working on a website (not a Chrome Extension), these files are not delivered by that site and can be safely ignored.
Also... a handful of images won't cause any meaningful lag on a modern browser.

localstorage or sessionstorage can't persist data on cross browser

I am working on a small application but I am stuck on a problem. I want stored form element values on a HTML page when filled in on one browser(Ex. Firefox) and auto fill data when same page is loaded in another browser(Ex. Chrome). If anybody has any ideas please help me.
Unless clients can login and you're willing to share this data via your server, you can not change behavior of a different browser from your current, so in your example Firefox can not change a cookie, localstorage or whatever of Chrome. Browsers tend to only share information like cookies when they are first ran; such as with you the import wizard from Firefox.
I can think of two alternatives to achieve this:
An authentication system where the data is stored server-side.
Through custom browser extensions. You could create a custom browser extension that directly writes the data of the other browsers. This does require the user to install that extension though.
This link explain how to achieve that http://www.nczonline.net/blog/2010/09/07/learning-from-xauth-cross-domain-localstorage/
It's not simple, but it's the way that I know it can be done at the moment without the use of cookies.

How to sync chrome extension options

I've made a Chrome extension with an options page. The data is saved in localstorage and works just fine.
Chrome doesn't sync the localstorage to the cloud, just the extensions. This means that any user data will not sync to other computers of the same google account.
I can not find an API at http://developer.chrome.com/extensions/docs.html which allows me to sync the user-chosen preferences.
What methods do you suggest?
In the (hopefully near) future, you'll be You are now able to store stuff in chrome.storage.sync, and it will be synced automagically.
Unless you need something right now, do consider putting all your configurations in an single object, sometime later you'll be able to just sync it!
Edit: now this is available in stable Chrome!
Nathan Moos told you truth :) But you don't need to write it by oneself, you can use Ankit Ahuja solution from Sylebot extension. You will need file js/sync.js. Source code is great, and it works great!
You should be able to use HTML5 WebDatabases, but you cannot. See comments below.
You could serialize some of the options to a bookmark and tell the user to enable bookmark sync. Then, you could read from the bookmark or build a pseudo-"localStorage" object around the bookmark.
You could try to do it with Google Docs like Chrome does with bookmarks. I don't know how far the api reaches but this seems a rather basic operation.

Categories

Resources