/*************************************************************
* This script is developed by Arturs Sosins aka ar2rsawseen, http://webcodingeasy.com
* Fee free to distribute and modify code, but keep reference to its creator
*
* This class can be used to manipulate SharedObjects (aka Flash cookies)
* using swf file provided in package with different domain and namespace sstings.
* This class can set values from javascript, get saved values from
* SharedObjects (preserving data types) and delete SharedObjects.
*
* For more information, examples and online documentation visit:
* http://webcodingeasy.com/JS-classes/Manage-SharedObjects-using-Javascript
**************************************************************/
import flash.external.*;
var fcookie:SharedObject;
var do_debug:Boolean = false;
var name_space:String = "fcookie";
//setting external interace callbacks
ExternalInterface.addCallback("init", init);
ExternalInterface.addCallback("fcookie_check", checker);
ExternalInterface.addCallback("fcookie_set", setter);
ExternalInterface.addCallback("fcookie_get", getter);
ExternalInterface.addCallback("fcookie_get_all", get_all);
ExternalInterface.addCallback("fcookie_del", remover);
ExternalInterface.addCallback("fcookie_del_all", remove_all);
//test cookie saving
function init(host:String = "*", debug:Boolean = false, ns:String = "fcookie"):void{
do_debug = debug;
name_space = ns;
Security.allowDomain(host);
Security.allowInsecureDomain(host);
reload();
try{
fcookie.data.check_so = true;
} catch(error:Error){
log_error("Can't save data: " + error.message);
}
if(!fcookie.data.f_data)
{
try{
fcookie.data.f_data = new Object;
} catch(error:Error){
log_error("Can't save data: " + error.message);
}
}
saver();
log_error("Loaded");
}
function reload():void{
fcookie = null;
try{
fcookie = SharedObject.getLocal(name_space);
}catch(error:Error){
log_error("Can't get local: " + error.message);
}
}
//check if cookies can be saved
function checker():Boolean{
if (fcookie.data.check_so)
{
return true;
}
else
{
return false;
}
}
//set cookie using key and value for strings
function setter(key:String, val:Object):void{
try{
log_error("Saving " + key);
fcookie.data.f_data[key] = val;
} catch(error:Error){
log_error("Can't save data: " + error.message);
}
saver();
}
//get cookie value by key
function getter(key:String):Object{
reload();
log_error("Returning " + key);
return fcookie.data.f_data[key];
}
function get_all():Object{
reload();
log_error("Returning all");
return fcookie.data.f_data;
}
//delete cookie by key
function remover(key:String):void{
try{
log_error("Removing " + key);
delete fcookie.data.f_data[key];
} catch(error:Error){
log_error("Can't delete data: " + error.message);
}
saver();
}
function remove_all():void{
try{
log_error("Removing all");
fcookie.data.f_data = new Object;
} catch(error:Error){
log_error("Can't save data: " + error.message);
}
saver();
}
//save cookies (internal use)
function saver():void{
try{
fcookie.flush(100000);
} catch(error:Error){
log_error("Can't save data to disk: " + error.message);
}
}
function log_error(err:String):void
{
if(do_debug)
{
ExternalInterface.call("fcookie.log", err);
}
}
//init(); |