2019-02-03 12:51:15 +00:00
/ * \
title : $ : / p l u g i n s / t i d d l y w i k i / b r o w s e r - s t o r a g e / s t a r t u p . j s
type : application / javascript
module - type : startup
Startup initialisation
\ * /
( function ( ) {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict" ;
// Export name and synchronous status
exports . name = "browser-storage" ;
exports . platforms = [ "browser" ] ;
2020-08-11 11:32:39 +00:00
exports . after = [ "startup" ] ;
2019-02-03 12:51:15 +00:00
exports . synchronous = true ;
2019-03-01 22:06:14 +00:00
var ENABLED _TITLE = "$:/config/BrowserStorage/Enabled" ,
SAVE _FILTER _TITLE = "$:/config/BrowserStorage/SaveFilter" ,
2019-02-07 14:46:23 +00:00
QUOTA _EXCEEDED _ALERT _TITLE = "$:/config/BrowserStorage/QuotaExceededAlert" ,
DEFAULT _QUOTA _EXCEEDED _ALERT _PREFIX = "Quota exceeded attempting to store `" ,
DEFAULT _QUOTA _EXCEEDED _ALERT _SUFFIX = "` in browser local storage" ;
2019-02-07 14:35:25 +00:00
2019-02-03 12:51:15 +00:00
exports . startup = function ( ) {
2019-02-07 14:35:25 +00:00
var self = this ;
2019-02-03 12:51:15 +00:00
// Compute our prefix for local storage keys
2019-02-08 15:19:20 +00:00
var prefix = "tw5#" + window . location . pathname + "#" ;
2019-02-03 17:29:32 +00:00
// Make a logger
var logger = new $tw . utils . Logger ( "browser-storage" , {
colour : "cyan"
} ) ;
2019-02-07 14:35:25 +00:00
// Function to compile the filter
var filterFn ,
compileFilter = function ( ) {
filterFn = $tw . wiki . compileFilter ( $tw . wiki . getTiddlerText ( SAVE _FILTER _TITLE ) ) ;
}
compileFilter ( ) ;
2019-03-01 22:06:14 +00:00
// Listen for tm-clear-browser-storage messages
$tw . rootWidget . addEventListener ( "tm-clear-browser-storage" , function ( event ) {
$tw . wiki . addTiddler ( { title : ENABLED _TITLE , text : "no" } ) ;
2019-03-03 11:30:14 +00:00
clearLocalStorage ( ) ;
2019-03-01 22:06:14 +00:00
} ) ;
2019-02-03 12:51:15 +00:00
// Track tiddler changes
$tw . wiki . addEventListener ( "change" , function ( changes ) {
2019-03-01 22:06:14 +00:00
// Bail if browser storage is disabled
if ( $tw . wiki . getTiddlerText ( ENABLED _TITLE ) === "no" ) {
return ;
}
2019-02-07 14:35:25 +00:00
// Recompile the filter if it has changed
if ( changes [ SAVE _FILTER _TITLE ] ) {
compileFilter ( ) ;
}
// Filter the changes
var filteredChanges = filterFn . call ( $tw . wiki , function ( iterator ) {
$tw . utils . each ( changes , function ( change , title ) {
var tiddler = $tw . wiki . getTiddler ( title ) ;
iterator ( tiddler , title ) ;
} ) ;
} ) ;
$tw . utils . each ( filteredChanges , function ( title ) {
2019-03-01 22:06:14 +00:00
// Don't try to save changes to our enabled status
// (If it were enabled in the file but disabled in local storage then we might not realise that distributing a copy of the file would have local storage enabled for other users)
if ( title === ENABLED _TITLE ) {
return ;
}
2019-03-03 11:30:14 +00:00
// Save the tiddler
saveTiddlerToLocalStorage ( title , {
logger : logger ,
prefix : prefix
} ) ;
2019-02-03 12:51:15 +00:00
} ) ;
} ) ;
} ;
2019-03-03 11:30:14 +00:00
function saveTiddlerToLocalStorage ( title , options ) {
options = options || { } ;
// Get the tiddler
var tiddler = $tw . wiki . getTiddler ( title ) ;
if ( tiddler ) {
2019-05-29 08:37:48 +00:00
console . log ( "browser-storage: Saving" , title ) ;
2019-03-03 11:30:14 +00:00
// Get the JSON of the tiddler
var json = JSON . stringify ( tiddler . getFieldStrings ( ) ) ;
// Try to save it to local storage
try {
window . localStorage . setItem ( options . prefix + title , json ) ;
} catch ( e ) {
if ( e . name === "QuotaExceededError" ) {
// Complain if we failed
var msg = $tw . wiki . getTiddlerText ( QUOTA _EXCEEDED _ALERT _TITLE , DEFAULT _QUOTA _EXCEEDED _ALERT _PREFIX + title + DEFAULT _QUOTA _EXCEEDED _ALERT _SUFFIX ) ;
if ( options . logger ) {
2019-05-29 08:37:48 +00:00
options . logger . alert ( msg ) ;
2019-03-03 11:30:14 +00:00
}
// No point in keeping old values around for this tiddler
window . localStorage . removeItem ( options . prefix + title ) ;
} else {
2019-05-29 08:37:48 +00:00
console . log ( "Browser-storage error:" , e ) ;
2019-03-03 11:30:14 +00:00
}
}
} else {
2022-04-12 21:11:37 +00:00
// In local storage, use the special value of empty string to mark the tiddler as deleted
// On future page loads, if the tiddler is already gone from startup then the blank entry
// will be removed from localstorage. Otherwise, the tiddler will be deleted.
console . log ( "browser-storage: Blanking" , title ) ;
2019-05-29 08:37:48 +00:00
try {
2022-04-12 21:11:37 +00:00
window . localStorage . setItem ( options . prefix + title , "" ) ;
2019-05-29 08:37:48 +00:00
} catch ( e ) {
console . log ( "Browser-storage error:" , e ) ;
}
2019-03-03 11:30:14 +00:00
}
}
function clearLocalStorage ( ) {
var url = window . location . pathname ,
log = [ ] ;
2022-05-21 14:36:23 +00:00
// Step through each browser storage item
if ( window . localStorage ) {
for ( var index = window . localStorage . length - 1 ; index >= 0 ; index -- ) {
var key = window . localStorage . key ( index ) ,
parts = key . split ( "#" ) ;
// Delete it if it is ours
if ( parts [ 0 ] === "tw5" && parts [ 1 ] === url ) {
window . localStorage . removeItem ( key ) ;
}
2019-03-03 11:30:14 +00:00
}
}
}
2019-02-03 12:51:15 +00:00
} ) ( ) ;