A unf. social network done poorly.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.7KB

  1. // Storage API
  2. var store = (function(){
  3. var api = {},
  4. win = window,
  5. doc = win.document,
  6. sessionStorageName = 'sessionStorage',
  7. localStorageName = 'localStorage',
  8. globalStorageName = 'globalStorage',
  9. storage
  10. api.set = function(key, value) {}
  11. api.get = function(key) {}
  12. api.remove = function(key) {}
  13. api.clear = function() {}
  14. function serialize(value) {
  15. return JSON.stringify(value)
  16. }
  17. function deserialize(value) {
  18. if (typeof value != 'string') { return undefined }
  19. return JSON.parse(value)
  20. }
  21. if (sessionStorageName in win && win[sessionStorageName]) {
  22. storage = win[sessionStorageName]
  23. api.set = function(key, val) { storage[key] = serialize(val) }
  24. api.get = function(key) { return deserialize(storage[key]) }
  25. api.remove = function(key) { delete storage[key] }
  26. api.clear = function() { storage.clear() }
  27. } else if (localStorageName in win && win[localStorageName]) {
  28. storage = win[localStorageName]
  29. api.set = function(key, val) { storage[key] = serialize(val) }
  30. api.get = function(key) { return deserialize(storage[key]) }
  31. api.remove = function(key) { delete storage[key] }
  32. api.clear = function() { storage.clear() }
  33. } else if (globalStorageName in win && win[globalStorageName]) {
  34. storage = win[globalStorageName][win.location.hostname]
  35. api.set = function(key, val) { storage[key] = serialize(val) }
  36. api.get = function(key) { return deserialize(storage[key] && storage[key].value) }
  37. api.remove = function(key) { delete storage[key] }
  38. api.clear = function() { for (var key in storage ) { delete storage[key] } }
  39. } else if (doc.documentElement.addBehavior) {
  40. function getStorage() {
  41. if (storage) { return storage; }
  42. storage = doc.body.appendChild(doc.createElement('div'))
  43. storage.style.display = 'none'
  44. // See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
  45. // and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
  46. storage.addBehavior('#default#userData')
  47. storage.load(localStorageName)
  48. return storage;
  49. }
  50. api.set = function(key, val) {
  51. var storage = getStorage()
  52. storage.setAttribute(key, serialize(val))
  53. storage.save(localStorageName)
  54. }
  55. api.get = function(key) {
  56. var storage = getStorage()
  57. return deserialize(storage.getAttribute(key))
  58. }
  59. api.remove = function(key) {
  60. var storage = getStorage()
  61. storage.removeAttribute(key)
  62. storage.save(localStorageName)
  63. }
  64. api.clear = function() {
  65. var storage = getStorage()
  66. var attributes = storage.XMLDocument.documentElement.attributes;
  67. storage.load(localStorageName)
  68. for (var i=0, attr; attr = attributes[i]; i++) {
  69. storage.removeAttribute(attr.name)
  70. }
  71. storage.save(localStorageName)
  72. }
  73. }
  74. return api
  75. })();