Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
66af98c5
Commit
66af98c5
authored
Jun 14, 2017
by
Robert Knight
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert `localStorage` service to JS
parent
6d9bd921
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
35 deletions
+68
-35
local-storage.coffee
src/sidebar/local-storage.coffee
+0
-35
local-storage.js
src/sidebar/local-storage.js
+68
-0
No files found.
src/sidebar/local-storage.coffee
deleted
100644 → 0
View file @
6d9bd921
module
.
exports
=
[
'$window'
,
(
$window
)
->
# Detection is needed because we run often as a third party widget and
# third party storage blocking often blocks cookies and local storage
# https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js
storage
=
do
->
key
=
'hypothesis.testKey'
try
$window
.
localStorage
.
setItem
key
,
key
$window
.
localStorage
.
removeItem
key
$window
.
localStorage
catch
memoryStorage
=
{}
getItem
:
(
key
)
->
if
key
of
memoryStorage
then
memoryStorage
[
key
]
else
null
setItem
:
(
key
,
value
)
->
memoryStorage
[
key
]
=
value
removeItem
:
(
key
)
->
delete
memoryStorage
[
key
]
return
{
getItem
:
(
key
)
->
storage
.
getItem
key
getObject
:
(
key
)
->
json
=
storage
.
getItem
key
return
JSON
.
parse
json
if
json
null
setItem
:
(
key
,
value
)
->
storage
.
setItem
key
,
value
setObject
:
(
key
,
value
)
->
repr
=
JSON
.
stringify
value
storage
.
setItem
key
,
repr
removeItem
:
(
key
)
->
storage
.
removeItem
key
}
]
src/sidebar/local-storage.js
0 → 100644
View file @
66af98c5
'use strict'
;
/**
* Fallback in-memory store if `localStorage` is not read/writable.
*/
class
InMemoryStorage
{
constructor
()
{
this
.
_store
=
{};
}
getItem
(
key
)
{
return
(
key
in
this
.
_store
)
?
this
.
_store
[
key
]
:
null
;
}
setItem
(
key
,
value
)
{
this
.
_store
[
key
]
=
value
;
}
removeItem
(
key
)
{
delete
this
.
_store
[
key
];
}
}
/**
* A wrapper around the `localStorage` API which provides a fallback to
* in-memory storage in browsers that block access to `window.localStorage`.
* in third-party iframes.
*/
// @ngInject
function
localStorage
(
$window
)
{
var
storage
=
$window
.
localStorage
;
try
{
// Test whether we can read/write localStorage.
var
key
=
'hypothesis.testKey'
;
$window
.
localStorage
.
setItem
(
key
,
key
);
$window
.
localStorage
.
getItem
(
key
);
$window
.
localStorage
.
removeItem
(
key
);
}
catch
(
e
)
{
storage
=
new
InMemoryStorage
();
}
return
{
getItem
(
key
)
{
return
storage
.
getItem
(
key
);
},
getObject
(
key
)
{
var
item
=
storage
.
getItem
(
key
);
return
item
?
JSON
.
parse
(
item
)
:
null
;
},
setItem
(
key
,
value
)
{
storage
.
setItem
(
key
,
value
);
},
setObject
(
key
,
value
)
{
var
repr
=
JSON
.
stringify
(
value
);
storage
.
setItem
(
key
,
repr
);
},
removeItem
(
key
)
{
storage
.
removeItem
(
key
);
},
};
}
module
.
exports
=
localStorage
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment