2015-01-30 17:07:24 +00:00
var stylishDb = null ;
function getDatabase ( ready , error ) {
if ( stylishDb != null && stylishDb . version == "1.5" ) {
ready ( stylishDb ) ;
return ;
}
try {
stylishDb = openDatabase ( 'stylish' , '' , 'Stylish Styles' , 5 * 1024 * 1024 ) ;
} catch ( ex ) {
error ( ) ;
throw ex ;
}
if ( stylishDb . version == "1.0" || stylishDb . version == "" ) {
dbV11 ( stylishDb , error , ready ) ;
} else if ( stylishDb . version == "1.1" ) {
dbV12 ( stylishDb , error , ready ) ;
} else if ( stylishDb . version == "1.2" ) {
dbV13 ( stylishDb , error , ready ) ;
} else if ( stylishDb . version == "1.3" ) {
dbV14 ( stylishDb , error , ready ) ;
} else if ( stylishDb . version == "1.4" ) {
dbV15 ( stylishDb , error , ready ) ;
} else {
2015-02-09 04:02:08 +00:00
ready ( stylishDb ) ;
}
}
2015-01-30 17:07:24 +00:00
function dbV11 ( d , error , done ) {
d . changeVersion ( d . version , '1.1' , function ( t ) {
t . executeSql ( 'CREATE TABLE styles (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, url TEXT, updateUrl TEXT, md5Url TEXT, name TEXT NOT NULL, code TEXT NOT NULL, enabled INTEGER NOT NULL, originalCode TEXT NULL);' ) ;
t . executeSql ( 'CREATE TABLE style_meta (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, style_id INTEGER NOT NULL, name TEXT NOT NULL, value TEXT NOT NULL);' ) ;
t . executeSql ( 'CREATE INDEX style_meta_style_id ON style_meta (style_id);' ) ;
} , error , function ( ) { dbV12 ( d , error , done ) } ) ;
}
function dbV12 ( d , error , done ) {
d . changeVersion ( d . version , '1.2' , function ( t ) {
// add section table
t . executeSql ( 'CREATE TABLE sections (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, style_id INTEGER NOT NULL, code TEXT NOT NULL);' ) ;
t . executeSql ( 'INSERT INTO sections (style_id, code) SELECT id, code FROM styles;' ) ;
// switch meta to sections
t . executeSql ( 'DROP INDEX style_meta_style_id;' ) ;
t . executeSql ( 'CREATE TABLE section_meta (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, section_id INTEGER NOT NULL, name TEXT NOT NULL, value TEXT NOT NULL);' ) ;
t . executeSql ( 'INSERT INTO section_meta (section_id, name, value) SELECT s.id, sm.name, sm.value FROM sections s INNER JOIN style_meta sm ON sm.style_id = s.style_id;' ) ;
t . executeSql ( 'CREATE INDEX section_meta_section_id ON section_meta (section_id);' ) ;
t . executeSql ( 'DROP TABLE style_meta;' ) ;
// drop extra fields from styles table
t . executeSql ( 'CREATE TABLE newstyles (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, url TEXT, updateUrl TEXT, md5Url TEXT, name TEXT NOT NULL, enabled INTEGER NOT NULL);' ) ;
t . executeSql ( 'INSERT INTO newstyles (id, url, updateUrl, md5Url, name, enabled) SELECT id, url, updateUrl, md5Url, name, enabled FROM styles;' ) ;
t . executeSql ( 'DROP TABLE styles;' ) ;
t . executeSql ( 'ALTER TABLE newstyles RENAME TO styles;' ) ;
} , error , function ( ) { dbV13 ( d , error , done ) } ) ;
}
function dbV13 ( d , error , done ) {
d . changeVersion ( d . version , '1.3' , function ( t ) {
// clear out orphans
t . executeSql ( 'DELETE FROM section_meta WHERE section_id IN (SELECT sections.id FROM sections LEFT JOIN styles ON styles.id = sections.style_id WHERE styles.id IS NULL);' ) ;
t . executeSql ( 'DELETE FROM sections WHERE id IN (SELECT sections.id FROM sections LEFT JOIN styles ON styles.id = sections.style_id WHERE styles.id IS NULL);' ) ;
} , error , function ( ) { dbV14 ( d , error , done ) } ) ;
}
function dbV14 ( d , error , done ) {
d . changeVersion ( d . version , '1.4' , function ( t ) {
t . executeSql ( 'UPDATE styles SET url = null WHERE url = "undefined";' ) ;
} , error , function ( ) { dbV15 ( d , error , done ) } ) ;
}
function dbV15 ( d , error , done ) {
d . changeVersion ( d . version , '1.5' , function ( t ) {
t . executeSql ( 'ALTER TABLE styles ADD COLUMN originalMd5 TEXT NULL;' ) ;
2015-07-30 20:34:33 +00:00
} , error , function ( ) { done ( d ) ; } ) ;
2015-01-30 17:07:24 +00:00
}
function enableStyle ( id , enabled ) {
getDatabase ( function ( db ) {
db . transaction ( function ( t ) {
t . executeSql ( "UPDATE styles SET enabled = ? WHERE id = ?;" , [ enabled , id ] ) ;
} , reportError , function ( ) {
2016-01-30 23:08:10 +00:00
chrome . runtime . sendMessage ( { method : "styleChanged" } ) ;
chrome . runtime . sendMessage ( { method : "getStyles" , id : id } , function ( styles ) {
2015-01-30 17:07:24 +00:00
handleUpdate ( styles [ 0 ] ) ;
2015-03-24 14:07:59 +00:00
notifyAllTabs ( { method : "styleUpdated" , style : styles [ 0 ] } ) ;
2015-01-30 17:07:24 +00:00
} ) ;
} ) ;
} ) ;
}
function deleteStyle ( id ) {
getDatabase ( function ( db ) {
db . transaction ( function ( t ) {
t . executeSql ( 'DELETE FROM section_meta WHERE section_id IN (SELECT id FROM sections WHERE style_id = ?);' , [ id ] ) ;
t . executeSql ( 'DELETE FROM sections WHERE style_id = ?;' , [ id ] ) ;
t . executeSql ( "DELETE FROM styles WHERE id = ?;" , [ id ] ) ;
} , reportError , function ( ) {
2016-01-30 23:08:10 +00:00
chrome . runtime . sendMessage ( { method : "styleChanged" } ) ;
2015-01-30 17:07:24 +00:00
handleDelete ( id ) ;
2015-02-09 04:02:08 +00:00
notifyAllTabs ( { method : "styleDeleted" , id : id } ) ;
2015-01-30 17:07:24 +00:00
} ) ;
} ) ;
}
function reportError ( ) {
for ( i in arguments ) {
if ( "message" in arguments [ i ] ) {
//alert(arguments[i].message);
console . log ( arguments [ i ] . message ) ;
}
}
}
function getDomains ( url ) {
if ( url . indexOf ( "file:" ) == 0 ) {
return [ ] ;
}
2015-04-07 08:56:50 +00:00
var d = /.*?:\/*([^\/:]+)/ . exec ( url ) [ 1 ] ;
2015-01-30 17:07:24 +00:00
var domains = [ d ] ;
while ( d . indexOf ( "." ) != - 1 ) {
d = d . substring ( d . indexOf ( "." ) + 1 ) ;
domains . push ( d ) ;
}
return domains ;
}
2015-02-09 04:02:08 +00:00
function getType ( o ) {
if ( typeof o == "undefined" || typeof o == "string" ) {
return typeof o ;
}
if ( o instanceof Array ) {
return "array" ;
}
throw "Not supported - " + o ;
}
function isCheckbox ( el ) {
return el . nodeName . toLowerCase ( ) == "input" && "checkbox" == el . type . toLowerCase ( ) ;
}
2015-10-05 11:27:17 +00:00
// Accepts an array of pref names (values are fetched via prefs.get)
// and establishes a two-way connection between the document elements and the actual prefs
function setupLivePrefs ( IDs ) {
2015-10-05 11:12:24 +00:00
var localIDs = { } ;
IDs . forEach ( function ( id ) {
localIDs [ id ] = true ;
updateElement ( id ) . addEventListener ( "change" , function ( ) {
2015-10-05 11:27:17 +00:00
prefs . set ( this . id , isCheckbox ( this ) ? this . checked : this . value ) ;
2015-10-05 11:12:24 +00:00
} ) ;
} ) ;
2016-01-31 00:06:04 +00:00
chrome . runtime . onMessage . addListener ( function ( request ) {
2015-10-05 11:12:24 +00:00
if ( request . prefName in localIDs ) {
updateElement ( request . prefName ) ;
2015-02-09 04:02:08 +00:00
}
2015-10-05 11:12:24 +00:00
} ) ;
function updateElement ( id ) {
var el = document . getElementById ( id ) ;
2015-10-05 11:27:17 +00:00
el [ isCheckbox ( el ) ? "checked" : "value" ] = prefs . get ( id ) ;
2015-03-07 07:57:57 +00:00
el . dispatchEvent ( new Event ( "change" , { bubbles : true , cancelable : true } ) ) ;
2015-10-05 11:12:24 +00:00
return el ;
2015-02-17 19:50:51 +00:00
}
2015-02-09 04:02:08 +00:00
}
2015-03-02 22:31:23 +00:00
2015-10-05 11:12:24 +00:00
var prefs = chrome . extension . getBackgroundPage ( ) . prefs || new function Prefs ( ) {
var me = this ;
var defaults = {
2015-10-04 12:26:50 +00:00
"openEditInWindow" : false , // new editor opens in a own browser window
"windowPosition" : { } , // detached window position
"show-badge" : true , // display text on popup menu icon
"disableAll" : false , // boss key
"popup.breadcrumbs" : true , // display "New style" links as URL breadcrumbs
"popup.breadcrumbs.usePath" : false , // use URL path for "this URL"
"popup.enabledFirst" : true , // display enabled styles before disabled styles
"popup.stylesFirst" : true , // display enabled styles before disabled styles
"manage.onlyEnabled" : false , // display only enabled styles
"manage.onlyEdited" : false , // display only styles created locally
"editor.options" : { } , // CodeMirror.defaults.*
"editor.lineWrapping" : true , // word wrap
"editor.smartIndent" : true , // "smart" indent
"editor.indentWithTabs" : false , // smart indent with tabs
"editor.tabSize" : 4 , // tab width, in spaces
"editor.keyMap" : navigator . appVersion . indexOf ( "Windows" ) > 0 ? "sublime" : "default" ,
"editor.theme" : "default" , // CSS theme
"editor.beautify" : { // CSS beautifier
selector _separator _newline : true ,
newline _before _open _brace : false ,
newline _after _open _brace : true ,
newline _between _properties : true ,
newline _before _close _brace : true ,
newline _between _rules : false ,
end _with _newline : false
} ,
"editor.lintDelay" : 500 , // lint gutter marker update delay, ms
"editor.lintReportDelay" : 4500 , // lint report update delay, ms
2015-10-05 11:12:24 +00:00
} ;
var values = deepCopy ( defaults ) ;
2015-03-04 07:55:35 +00:00
2015-10-05 11:12:24 +00:00
var syncTimeout ; // see broadcast() function below
2015-03-02 22:31:23 +00:00
2015-10-05 11:12:24 +00:00
Object . defineProperty ( this , "readOnlyValues" , { value : { } } ) ;
2015-10-05 11:27:17 +00:00
Prefs . prototype . get = function ( key , defaultValue ) {
2015-10-05 11:12:24 +00:00
if ( key in values ) {
return values [ key ] ;
2015-03-02 22:31:23 +00:00
}
2015-10-05 11:12:24 +00:00
if ( defaultValue !== undefined ) {
return defaultValue ;
2015-03-02 22:31:23 +00:00
}
2015-10-05 11:12:24 +00:00
if ( key in defaults ) {
return defaults [ key ] ;
2015-03-07 07:57:57 +00:00
}
2015-10-05 11:12:24 +00:00
console . warn ( "No default preference for '%s'" , key ) ;
} ;
2015-10-05 11:27:17 +00:00
Prefs . prototype . getAll = function ( key ) {
2015-10-05 11:12:24 +00:00
return deepCopy ( values ) ;
} ;
2015-10-05 11:27:17 +00:00
Prefs . prototype . set = function ( key , value , options ) {
2015-10-05 11:12:24 +00:00
var oldValue = deepCopy ( values [ key ] ) ;
values [ key ] = value ;
defineReadonlyProperty ( this . readOnlyValues , key , value ) ;
if ( ( ! options || ! options . noBroadcast ) && ! equal ( value , oldValue ) ) {
me . broadcast ( key , value , options ) ;
2015-10-04 12:26:50 +00:00
}
2015-10-05 11:12:24 +00:00
} ;
2015-10-05 11:27:17 +00:00
Prefs . prototype . remove = function ( key ) { me . set ( key , undefined ) } ;
2015-10-05 11:12:24 +00:00
Prefs . prototype . broadcast = function ( key , value , options ) {
2015-10-04 12:26:50 +00:00
var message = { method : "prefChanged" , prefName : key , value : value } ;
notifyAllTabs ( message ) ;
2016-01-30 23:08:10 +00:00
chrome . runtime . sendMessage ( message ) ;
2015-10-05 11:12:24 +00:00
if ( key == "disableAll" ) {
notifyAllTabs ( { method : "styleDisableAll" , disableAll : value } ) ;
}
2015-10-04 12:26:50 +00:00
if ( ! options || ! options . noSync ) {
2015-10-05 11:12:24 +00:00
clearTimeout ( syncTimeout ) ;
syncTimeout = setTimeout ( function ( ) {
2016-01-30 23:59:45 +00:00
getSync ( ) . set ( { "settings" : values } ) ;
2015-10-05 11:12:24 +00:00
} , 0 ) ;
2015-03-07 07:57:57 +00:00
}
2015-10-05 11:12:24 +00:00
} ;
2015-05-05 15:21:45 +00:00
2015-10-05 11:12:24 +00:00
Object . keys ( defaults ) . forEach ( function ( key ) {
2015-10-05 11:27:17 +00:00
me . set ( key , defaults [ key ] , { noBroadcast : true } ) ;
2015-10-05 11:12:24 +00:00
} ) ;
2016-01-30 23:59:45 +00:00
getSync ( ) . get ( "settings" , function ( result ) {
2015-10-05 11:12:24 +00:00
var synced = result . settings ;
for ( var key in defaults ) {
if ( synced && ( key in synced ) ) {
2015-10-05 11:27:17 +00:00
me . set ( key , synced [ key ] , { noSync : true } ) ;
2015-10-05 11:12:24 +00:00
} else {
var value = tryMigrating ( key ) ;
if ( value !== undefined ) {
2015-10-05 11:27:17 +00:00
me . set ( key , value ) ;
2015-10-05 11:12:24 +00:00
}
}
2015-10-04 12:26:50 +00:00
}
} ) ;
2015-10-05 11:12:24 +00:00
chrome . storage . onChanged . addListener ( function ( changes , area ) {
if ( area == "sync" && "settings" in changes ) {
var synced = changes . settings . newValue ;
if ( synced ) {
for ( key in defaults ) {
if ( key in synced ) {
2015-10-05 11:27:17 +00:00
me . set ( key , synced [ key ] , { noSync : true } ) ;
2015-10-05 11:12:24 +00:00
}
}
} else {
// user manually deleted our settings, we'll recreate them
2016-01-30 23:59:45 +00:00
getSync ( ) . set ( { "settings" : values } ) ;
2015-10-04 12:26:50 +00:00
}
}
2015-10-05 11:12:24 +00:00
} ) ;
2015-10-04 12:26:50 +00:00
2015-10-05 11:12:24 +00:00
function tryMigrating ( key ) {
if ( ! ( key in localStorage ) ) {
return undefined ;
}
var value = localStorage [ key ] ;
delete localStorage [ key ] ;
localStorage [ "DEPRECATED: " + key ] = value ;
switch ( typeof defaults [ key ] ) {
case "boolean" :
return value . toLowerCase ( ) === "true" ;
case "number" :
return Number ( value ) ;
case "object" :
try {
return JSON . parse ( value ) ;
} catch ( e ) {
console . log ( "Cannot migrate from localStorage %s = '%s': %o" , key , value , e ) ;
return undefined ;
}
}
return value ;
2015-10-04 12:26:50 +00:00
}
2015-10-05 11:12:24 +00:00
} ;
2015-10-04 12:26:50 +00:00
2015-05-05 15:21:45 +00:00
function getCodeMirrorThemes ( callback ) {
chrome . runtime . getPackageDirectoryEntry ( function ( rootDir ) {
rootDir . getDirectory ( "codemirror/theme" , { create : false } , function ( themeDir ) {
themeDir . createReader ( ) . readEntries ( function ( entries ) {
2015-05-05 20:42:38 +00:00
var themes = [ chrome . i18n . getMessage ( "defaultTheme" ) ] ;
2015-05-05 15:21:45 +00:00
entries
. filter ( function ( entry ) { return entry . isFile } )
. sort ( function ( a , b ) { return a . name < b . name ? - 1 : 1 } )
. forEach ( function ( entry ) {
themes . push ( entry . name . replace ( /\.css$/ , "" ) ) ;
} ) ;
if ( callback ) {
callback ( themes ) ;
}
} ) ;
} ) ;
} ) ;
}
2015-05-27 19:02:36 +00:00
function sessionStorageHash ( name ) {
var hash = {
value : { } ,
set : function ( k , v ) { this . value [ k ] = v ; this . updateStorage ( ) ; } ,
unset : function ( k ) { delete this . value [ k ] ; this . updateStorage ( ) ; } ,
updateStorage : function ( ) {
sessionStorage [ this . name ] = JSON . stringify ( this . value ) ;
}
} ;
try { hash . value = JSON . parse ( sessionStorage [ name ] ) ; } catch ( e ) { }
Object . defineProperty ( hash , "name" , { value : name } ) ;
return hash ;
}
2015-06-10 17:15:50 +00:00
2015-10-05 11:12:24 +00:00
function deepCopy ( obj ) {
if ( ! obj || typeof obj != "object" ) {
return obj ;
} else {
var emptyCopy = Object . create ( Object . getPrototypeOf ( obj ) ) ;
return deepMerge ( emptyCopy , obj ) ;
}
2015-07-13 17:44:46 +00:00
}
2015-10-05 11:12:24 +00:00
function deepMerge ( target , obj1 /* plus any number of object arguments */ ) {
for ( var i = 1 ; i < arguments . length ; i ++ ) {
var obj = arguments [ i ] ;
for ( var k in obj ) {
// hasOwnProperty checking is not needed for our non-OOP stuff
var value = obj [ k ] ;
if ( ! value || typeof value != "object" ) {
target [ k ] = value ;
} else if ( k in target ) {
deepMerge ( target [ k ] , value ) ;
} else {
target [ k ] = deepCopy ( value ) ;
}
2015-07-13 17:44:46 +00:00
}
2015-06-10 17:15:50 +00:00
}
2015-10-05 11:12:24 +00:00
return target ;
}
function shallowMerge ( target , obj1 /* plus any number of object arguments */ ) {
for ( var i = 1 ; i < arguments . length ; i ++ ) {
var obj = arguments [ i ] ;
for ( var k in obj ) {
target [ k ] = obj [ k ] ;
// hasOwnProperty checking is not needed for our non-OOP stuff
}
}
return target ;
2015-06-10 17:15:50 +00:00
}
function equal ( a , b ) {
2015-07-26 02:24:18 +00:00
if ( ! a || ! b || typeof a != "object" || typeof b != "object" ) {
2015-06-10 17:15:50 +00:00
return a === b ;
}
if ( Object . keys ( a ) . length != Object . keys ( b ) . length ) {
return false ;
}
for ( var k in a ) {
if ( a [ k ] !== b [ k ] ) {
return false ;
}
}
return true ;
}
2015-10-05 11:12:24 +00:00
function defineReadonlyProperty ( obj , key , value ) {
var copy = deepCopy ( value ) ;
Object . freeze ( copy ) ;
Object . defineProperty ( obj , key , { value : copy , configurable : true } )
}
2016-01-30 23:59:45 +00:00
// Polyfill, can be removed when Firefox gets this - https://bugzilla.mozilla.org/show_bug.cgi?id=1220494
function getSync ( ) {
if ( "sync" in chrome . storage ) {
return chrome . storage . sync ;
}
crappyStorage = { } ;
return {
get : function ( key , callback ) {
callback ( crappyStorage [ key ] || { } ) ;
} ,
set : function ( source , callback ) {
for ( var property in source ) {
if ( source . hasOwnProperty ( property ) ) {
crappyStorage [ property ] = source [ property ] ;
}
}
callback ( ) ;
}
}
}