Chromium trioxide - Plating your chrome extension
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
.gitignore
.jshintignore
.jshintrc
Makefile
README.md
cro3.background-min.js
cro3.background.js
cro3.frontend-min.js
cro3.frontend.js
package.json

README.md

Chromium trioxide

Plating your chrome extension

Usage

Include cro3.background-min.js in background and cro3.frontend-min.js in frontend (e.g. options page):

// manifest.json
{
  "name": "MyApp",
  "version": "0.0.0",
  "manifest_version": 2,
  "background": {
    "scripts": [
      "cro3.background-min.js",
      "background.js"
    ]
  },
  "options_page": "options.html"
}

Create background.js like:

new CrO3.Background({
  name: {
    // with validation
    default: "Chromium trioxide",
    validate: function(value) {
      if (value.length > 20) {
        return "Too Long";
      }
    }
  },
  author: "Yuku Takahashi" // without validation
});

And options.html:

<!DOCTYPE html>
<html>
  <head>
    <script src="jquery.js"></script>
    <script src="cro3.frontend-min.js"></script>
    <script>
      $('input').setting({
        auto_save: true,
        error: function(msg) {
          alert(msg);
        }
      });
    </script>
  </head>
  <body>
    <input name="name"/>
    <input name="author"/>
  </body>
</html>

Here, frontend script loads name and author from background and put them into value attribute of each input tag. Since { auto_save: true } is passed (default true) CrO3 automatically save the value on every 'change' event triggered to input tags. If the length of value attribute of input[name='name'] tag is longer than 20, alerts "{ name: 'Too Long' }" (See background.js above).

CrO3.Background

Defined in cro3.background.js

Background manager class. You may simply create an instance of this class in background script.

Constructor accepts an object and treats the keys of the given object as the list of settings. If the value is an object with default key, the value corresponds to the key is used as default value; otherwise the object is.

In addition, if the object has validate, the given value will be validated using it. If the value is valid, don't return.

CrO3.Setting

Defined in cro3.frontend.js

Client class.

var setting = new CrO3.setting;

get setting.get(attr)

Get the setting value from background. It returns jQuery.Deferred.promise():

setting
  .get('name')
  .done(function(value)  { /* Callback */ })
  .fail(function(errors) { /* Callback */ })
  .then(function(msg)    { /* Callback */ });

set setting.set(attrs)

Set the setting value. It returns jQuery.Deferred.promise():

setting.set('name', 'Chromium');

unset setting.unset(attr)

Unset the setting value. If attr has default, the value will be set as the default. It returns jQuery.Deferred.promise():

setting.unset('name');

keys setting.keys()

Get the list of setting keys. It returns jQuery.Deferred.promise():

setting
  .keys()
  .done(function(keys)  { /* Callback */ })

jQuery.fn.setting

Defined in cro3.frontend.js

jQuery plugin to easily create options page.

Licensed

Licensed under MIT license

Copyright (c) 2012 Yuku Takahashi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.