@ -4,64 +4,63 @@ var useragent_options = ['', 'googlebot', 'bingbot'];
var referer _options = [ '' , 'facebook' , 'google' , 'twitter' ] ;
function capitalize ( str ) {
if ( typeof str !== 'string' ) return '' ;
return str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) ;
return ( typeof str === 'string' ) ? str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) : '' ;
}
function sortJson ( json ) {
return Object . keys ( json )
. sort ( ) . reduce ( function ( Obj , key ) {
Obj [ key ] = json [ key ] ;
return Obj ;
} , { } ) ;
return Object . keys ( json )
. sort ( ) . reduce ( function ( Obj , key ) {
Obj [ key ] = json [ key ] ;
return Obj ;
} , { } ) ;
}
// Saves options to ext_api.storage
function save _options ( ) {
var textareaEl = document . querySelector ( '#bypass_sites textarea' ) ;
var sites _custom = { } ;
if ( textareaEl . value !== '' )
var sites _custom = JSON . parse ( textareaEl . value ) ;
ext _api . storage . local . set ( {
sites _custom : sites _custom
} , function ( ) {
// Update status to let user know custom sites were saved.
var status = document . getElementById ( 'status' ) ;
status . textContent = 'Custom sites saved.' ;
setTimeout ( function ( ) {
status . textContent = '' ;
location . href = 'options.html' ;
//window.close();
} , 800 ) ;
} ) ;
var textareaEl = document . querySelector ( '#bypass_sites textarea' ) ;
var sites _custom = { } ;
if ( textareaEl . value !== '' )
var sites _custom = JSON . parse ( textareaEl . value ) ;
ext _api . storage . local . set ( {
sites _custom : sites _custom
} , function ( ) {
// Update status to let user know custom sites were saved.
var status = document . getElementById ( 'status' ) ;
status . textContent = 'Custom sites saved.' ;
setTimeout ( function ( ) {
status . textContent = '' ;
location . href = 'options.html' ;
//window.close();
} , 800 ) ;
} ) ;
}
// Sort json by key in textarea
function sort _options ( ) {
var textareaEl = document . querySelector ( '#bypass_sites textarea' ) ;
var sites _custom = { } ;
if ( textareaEl . value !== '' ) {
var sites _custom = JSON . parse ( textareaEl . value ) ;
var sites _custom _sorted = sortJson ( sites _custom ) ;
textareaEl . value = JSON . stringify ( sites _custom _sorted ) ;
}
var textareaEl = document . querySelector ( '#bypass_sites textarea' ) ;
var sites _custom = { } ;
if ( textareaEl . value !== '' ) {
var sites _custom = JSON . parse ( textareaEl . value ) ;
var sites _custom _sorted = sortJson ( sites _custom ) ;
textareaEl . value = JSON . stringify ( sites _custom _sorted ) ;
}
}
// Export custom sites to file
function export _options ( ) {
ext _api . storage . local . get ( {
sites _custom : { }
} , function ( items ) {
var result = JSON . stringify ( items . sites _custom ) ;
var a = document . createElement ( "a" ) ;
var file = new Blob ( [ result ] , { type : "text/plain" } ) ;
a . href = window . URL . createObjectURL ( file ) ;
let date = new Date ( ) ;
let dateStr = new Date ( date . getTime ( ) - ( date . getTimezoneOffset ( ) * 60000 ) ) . toISOString ( ) . split ( "T" ) [ 0 ] ;
a . download = 'bypass_paywalls_clean_custom_' + dateStr + '.txt' ;
a . click ( ) ;
} ) ;
ext _api . storage . local . get ( {
sites _custom : { }
} , function ( items ) {
var result = JSON . stringify ( items . sites _custom ) ;
var a = document . createElement ( "a" ) ;
var file = new Blob ( [ result ] , { type : "text/plain" } ) ;
a . href = window . URL . createObjectURL ( file ) ;
let date = new Date ( ) ;
let dateStr = new Date ( date . getTime ( ) - ( date . getTimezoneOffset ( ) * 60000 ) ) . toISOString ( ) . split ( "T" ) [ 0 ] ;
a . download = 'bypass_paywalls_clean_custom_' + dateStr + '.txt' ;
a . click ( ) ;
} ) ;
}
// Import custom sites from file
@ -73,266 +72,269 @@ function import_options(e) {
}
function _imp ( ) {
let sites _custom = JSON . parse ( this . result ) ;
var result = this . result ;
ext _api . storage . local . get ( {
sites _custom : { }
} , function ( items ) {
var sites _custom = items . sites _custom ;
var sites _custom _new = JSON . parse ( result ) ;
for ( let site in sites _custom _new )
sites _custom [ site ] = sites _custom _new [ site ] ;
ext _api . storage . local . set ( {
sites _custom : sites _custom
sites _custom : sites _custom
} , function ( ) {
// Update status to let user know custom sites were imported.
var status = document . getElementById ( 'status' ) ;
status . textContent = 'Custom sites imported.' ;
setTimeout ( function ( ) {
//status.textContent = '';
importInput . value = '' ;
renderOptions ( ) ;
} , 800 ) ;
// Update status to let user know custom sites were imported.
var status = document . getElementById ( 'status' ) ;
status . textContent = 'Custom sites imported.' ;
setTimeout ( function ( ) {
//status.textContent = '';
importInput . value = '' ;
renderOptions ( ) ;
} , 800 ) ;
} ) ;
} ) ;
}
// Add custom site to ext_api.storage
function add _options ( ) {
var inputEls = document . querySelectorAll ( '#add_site input, #add_site select' ) ;
var sites _custom = { } ;
for ( let i = 0 ; i < inputEls . length ; i ++ ) {
if ( inputEls [ i ] . dataset . key === 'title' ) {
var title = capitalize ( inputEls [ i ] . value ) ;
if ( title === '' )
break ;
sites _custom [ title ] = { } ;
} else {
if ( inputEls [ i ] . dataset . value ) {
if ( inputEls [ i ] . checked )
sites _custom [ title ] [ inputEls [ i ] . dataset . key ] = inputEls [ i ] . dataset . value ;
} else if ( inputEls [ i ] . value )
sites _custom [ title ] [ inputEls [ i ] . dataset . key ] = inputEls [ i ] . value ;
}
}
if ( title && sites _custom [ title ] [ 'domain' ] ) {
sites _custom [ title ] [ 'domain' ] = sites _custom [ title ] [ 'domain' ] . replace ( /(http(s)?:\/\/(www\.)?|^www\.|\/$)/g , '' ) . toLowerCase ( ) ;
// add new site to local storage
ext _api . storage . local . get ( {
sites _custom : { }
} , function ( items ) {
var sites _custom _old = items . sites _custom ;
for ( var key in sites _custom ) {
sites _custom _old [ key ] = sites _custom [ key ] ;
}
ext _api . storage . local . set ( {
sites _custom : sites _custom _old
} , function ( ) {
// Update status to let user know new custom site was added.
var status _add = document . getElementById ( 'status_add' ) ;
status _add . textContent = 'Site added.' ;
setTimeout ( function ( ) {
//status.textContent = '';
renderOptions ( ) ;
} , 800 ) ;
} ) ;
} ) ;
var inputEls = document . querySelectorAll ( '#add_site input, #add_site select' ) ;
var sites _custom = { } ;
for ( let elem of inputEls ) {
if ( elem . dataset . key === 'title' ) {
var title = capitalize ( elem . value ) ;
if ( title === '' )
break ;
sites _custom [ title ] = { } ;
} else {
if ( elem . dataset . value ) {
if ( elem . checked )
sites _custom [ title ] [ elem . dataset . key ] = elem . dataset . value ;
} else if ( elem . value )
sites _custom [ title ] [ elem . dataset . key ] = elem . value ;
}
}
if ( title && sites _custom [ title ] [ 'domain' ] ) {
sites _custom [ title ] [ 'domain' ] = sites _custom [ title ] [ 'domain' ] . replace ( /(http(s)?:\/\/(www\.)?|^www\.|\/$)/g , '' ) . toLowerCase ( ) ;
// add new site to local storage
ext _api . storage . local . get ( {
sites _custom : { }
} , function ( items ) {
var sites _custom _old = items . sites _custom ;
for ( var key in sites _custom ) {
sites _custom _old [ key ] = sites _custom [ key ] ;
}
ext _api . storage . local . set ( {
sites _custom : sites _custom _old
} , function ( ) {
// Update status to let user know new custom site was added.
var status _add = document . getElementById ( 'status_add' ) ;
status _add . textContent = 'Site added.' ;
setTimeout ( function ( ) {
//status.textContent = '';
renderOptions ( ) ;
} , 800 ) ;
} ) ;
} ) ;
}
}
// Delete custom site from ext_api.storage
function delete _options ( ) {
var selectEl = document . querySelector ( '#custom_sites select' ) ;
var sites _custom = { } ;
var remove _key = selectEl . value ;
// delete site from local storage
ext _api . storage . local . get ( {
sites _custom : { }
} , function ( items ) {
var sites _custom _old = items . sites _custom ;
delete sites _custom _old [ remove _key ] ;
ext _api . storage . local . set ( {
sites _custom : sites _custom _old
} , function ( ) {
// Update status to let user know custom site was deleted.
var status _delete = document . getElementById ( 'status_delete' ) ;
status _delete . textContent = 'Site deleted.' ;
setTimeout ( function ( ) {
//status.textContent = '';
renderOptions ( ) ;
} , 800 ) ;
} ) ;
var selectEl = document . querySelector ( '#custom_sites select' ) ;
var sites _custom = { } ;
var remove _key = selectEl . value ;
// delete site from local storage
ext _api . storage . local . get ( {
sites _custom : { }
} , function ( items ) {
var sites _custom _old = items . sites _custom ;
delete sites _custom _old [ remove _key ] ;
ext _api . storage . local . set ( {
sites _custom : sites _custom _old
} , function ( ) {
// Update status to let user know custom site was deleted.
var status _delete = document . getElementById ( 'status_delete' ) ;
status _delete . textContent = 'Site deleted.' ;
setTimeout ( function ( ) {
//status.textContent = '';
renderOptions ( ) ;
} , 800 ) ;
} ) ;
} ) ;
}
// Edit custom site (copy to add)
function edit _options ( ) {
var selectEl = document . querySelector ( '#custom_sites select' ) ;
var sites _custom = { } ;
var title = selectEl . value ;
// copy site to add-fields
ext _api . storage . local . get ( {
sites _custom : { }
} , function ( items ) {
sites _custom = items . sites _custom ;
var edit _site = sites _custom [ title ] ;
document . querySelector ( 'input[data-key="title"]' ) . value = title ;
document . querySelector ( 'input[data-key="domain"]' ) . value = edit _site . domain ;
document . querySelector ( 'select[data-key="useragent"]' ) . selectedIndex = ( edit _site . googlebot > 0 ) ? 1 : useragent _options . indexOf ( edit _site . useragent ) ;
document . querySelector ( 'input[data-key="allow_cookies"]' ) . checked = ( edit _site . allow _cookies > 0 ) ;
document . querySelector ( 'input[data-key="block_javascript"]' ) . checked = ( edit _site . block _javascript > 0 ) ;
document . querySelector ( 'input[data-key="block_javascript_ext"]' ) . checked = ( edit _site . block _javascript _ext > 0 ) ;
document . querySelector ( 'select[data-key="referer"]' ) . selectedIndex = referer _options . indexOf ( edit _site . referer ) ;
} ) ;
var selectEl = document . querySelector ( '#custom_sites select' ) ;
var sites _custom = { } ;
var title = selectEl . value ;
// copy site to add-fields
ext _api . storage . local . get ( {
sites _custom : { }
} , function ( items ) {
sites _custom = items . sites _custom ;
var edit _site = sites _custom [ title ] ;
document . querySelector ( 'input[data-key="title"]' ) . value = title ;
document . querySelector ( 'input[data-key="domain"]' ) . value = edit _site . domain ;
document . querySelector ( 'select[data-key="useragent"]' ) . selectedIndex = ( edit _site . googlebot > 0 ) ? 1 : useragent _options . indexOf ( edit _site . useragent ) ;
document . querySelector ( 'input[data-key="allow_cookies"]' ) . checked = ( edit _site . allow _cookies > 0 ) ;
document . querySelector ( 'input[data-key="block_javascript"]' ) . checked = ( edit _site . block _javascript > 0 ) ;
document . querySelector ( 'input[data-key="block_javascript_ext"]' ) . checked = ( edit _site . block _javascript _ext > 0 ) ;
document . querySelector ( 'input[data-key="block_regex"]' ) . value = edit _site . block _regex ? edit _site . block _regex : '' ;
document . querySelector ( 'select[data-key="referer"]' ) . selectedIndex = referer _options . indexOf ( edit _site . referer ) ;
} ) ;
}
// request permissions for custom sites (in list only)
function request _permissions ( ) {
var perm _custom = document . getElementById ( 'perm-custom' ) ;
ext _api . permissions . request ( {
origins : perm _origins
} , function ( granted ) {
if ( granted ) {
perm _custom . innerText = 'YES' ;
} else {
perm _custom . innerText = 'NO' ;
}
} ) ;
var perm _custom = document . getElementById ( 'perm-custom' ) ;
ext _api . permissions . request ( {
origins : perm _origins
} , function ( granted ) {
if ( granted ) {
perm _custom . innerText = 'YES' ;
} else {
perm _custom . innerText = 'NO' ;
}
} ) ;
}
// remove permissions for custom sites
function remove _permissions ( ) {
var perm _custom = document . getElementById ( 'perm-custom' ) ;
ext _api . permissions . remove ( {
origins : perm _origins
} , function ( removed ) {
if ( removed ) {
perm _custom . innerText = 'NO' ;
}
} ) ;
var perm _custom = document . getElementById ( 'perm-custom' ) ;
ext _api . permissions . remove ( {
origins : perm _origins
} , function ( removed ) {
if ( removed ) {
perm _custom . innerText = 'NO' ;
}
} ) ;
}
var perm _origins ;
// Restores checkbox input states using the preferences stored in ext_api.storage.
function renderOptions ( ) {
ext _api . storage . local . get ( {
sites _custom : { }
} , function ( items ) {
var sites _custom = items . sites _custom ;
var sitesEl = document . getElementById ( 'bypass_sites' ) ;
sitesEl . innerHTML = '' ;
var labelEl = document . createElement ( 'label' ) ;
var textareaEl = document . createElement ( 'textarea' ) ;
textareaEl . value = JSON . stringify ( sites _custom ) ;
textareaEl . rows = 12 ;
textareaEl . cols = 40 ;
labelEl . appendChild ( textareaEl ) ;
sitesEl . appendChild ( labelEl ) ;
// add site
var add _sitesEl = document . getElementById ( 'add_site' ) ;
add _sitesEl . innerHTML = '' ;
var inputEl ;
var add _checkboxes = {
'title' : 0 ,
'domain' : 0 ,
'allow_cookies' : 1 ,
'block_javascript' : 1 ,
'block_javascript_ext' : 1
} ;
for ( var key in add _checkboxes ) {
labelEl = document . createElement ( 'label' ) ;
inputEl = document . createElement ( 'input' ) ;
inputEl . dataset . key = key ;
labelEl . appendChild ( inputEl ) ;
if ( add _checkboxes [ key ] ) {
inputEl . type = 'checkbox' ;
inputEl . dataset . value = 1 ;
} else if ( key === 'title' ) {
inputEl . placeholder = 'Example' ;
} else if ( key === 'domain' )
inputEl . placeholder = 'example.com' ;
labelEl . appendChild ( document . createTextNode ( ' ' + key ) ) ;
add _sitesEl . appendChild ( labelEl ) ;
}
labelEl = document . createElement ( 'label' ) ;
labelEl . appendChild ( document . createTextNode ( 'useragent ' ) ) ;
inputEl = document . createElement ( 'select' ) ;
inputEl . dataset . key = 'useragent' ;
labelEl . appendChild ( inputEl ) ;
for ( var i = 0 ; i < useragent _options . length ; i ++ ) {
var option = document . createElement ( "option" ) ;
option . value = useragent _options [ i ] ;
option . text = useragent _options [ i ] ;
inputEl . appendChild ( option ) ;
}
add _sitesEl . appendChild ( labelEl ) ;
labelEl = document . createElement ( 'label' ) ;
labelEl . appendChild ( document . createTextNode ( 'referer ' ) ) ;
inputEl = document . createElement ( 'select' ) ;
inputEl . dataset . key = 'referer' ;
labelEl . appendChild ( inputEl ) ;
for ( var i = 0 ; i < referer _options . length ; i ++ ) {
var option = document . createElement ( "option" ) ;
option . value = referer _options [ i ] ;
option . text = referer _options [ i ] ;
inputEl . appendChild ( option ) ;
}
add _sitesEl . appendChild ( labelEl ) ;
// list of custom sites
var custom _sitesEl = document . getElementById ( 'custom_sites' ) ;
custom _sitesEl . innerHTML = '' ;
labelEl = document . createElement ( 'label' ) ;
var selectEl = document . createElement ( 'select' ) ;
selectEl . id = 'sites' ;
selectEl . size = 6 ;
var optionEl ;
perm _origins = [ ] ;
for ( var key in sites _custom ) {
optionEl = document . createElement ( 'option' ) ;
let domain = sites _custom [ key ] [ 'domain' ] ;
perm _origins . push ( '*://*.' + domain + '/*' ) ;
let isDefaultSite = defaultSites _domains . includes ( domain ) ;
optionEl . text = isDefaultSite ? '*' : '' ;
optionEl . text += key + ': ' + domain +
( sites _custom [ key ] [ 'googlebot' ] > 0 ? ' | googlebot' : '' ) +
( sites _custom [ key ] [ 'allow_cookies' ] > 0 ? ' | allow_cookies' : '' ) +
( sites _custom [ key ] [ 'block_javascript' ] > 0 ? ' | block javascript' : '' ) +
( sites _custom [ key ] [ 'block_javascript_ext' ] > 0 ? ' | block javascript ext' : '' ) +
( sites _custom [ key ] [ 'useragent' ] ? ' | useragent: ' + sites _custom [ key ] [ 'useragent' ] : '' ) +
( sites _custom [ key ] [ 'referer' ] ? ' | referer: ' + sites _custom [ key ] [ 'referer' ] : '' ) ;
optionEl . value = key ;
selectEl . add ( optionEl ) ;
}
labelEl . appendChild ( selectEl ) ;
custom _sitesEl . appendChild ( labelEl ) ;
var perm _custom = document . getElementById ( 'perm-custom' ) ;
ext _api . permissions . contains ( {
origins : perm _origins
} , function ( result ) {
if ( result ) {
perm _custom . innerText = 'YES' ;
} else {
perm _custom . innerText = 'NO' ;
}
} ) ;
} ) ;
var custom _enabled = document . getElementById ( 'custom-enabled' ) ;
ext _api . storage . local . get ( {
sites _custom : { }
} , function ( items ) {
var sites _custom = items . sites _custom ;
var sitesEl = document . getElementById ( 'bypass_sites' ) ;
sitesEl . innerHTML = '' ;
var labelEl = document . createElement ( 'label' ) ;
var textareaEl = document . createElement ( 'textarea' ) ;
textareaEl . value = JSON . stringify ( sites _custom ) ;
textareaEl . rows = 12 ;
textareaEl . cols = 40 ;
labelEl . appendChild ( textareaEl ) ;
sitesEl . appendChild ( labelEl ) ;
// add site
var add _sitesEl = document . getElementById ( 'add_site' ) ;
add _sitesEl . innerHTML = '' ;
var inputEl ;
var add _checkboxes = {
'title' : 0 ,
'domain' : 0 ,
'allow_cookies' : 1 ,
'block_javascript' : 1 ,
'block_javascript_ext' : 1 ,
'block_regex' : 0
} ;
for ( var key in add _checkboxes ) {
labelEl = document . createElement ( 'label' ) ;
inputEl = document . createElement ( 'input' ) ;
inputEl . dataset . key = key ;
labelEl . appendChild ( inputEl ) ;
if ( add _checkboxes [ key ] ) {
inputEl . type = 'checkbox' ;
inputEl . dataset . value = 1 ;
} else if ( key === 'title' ) {
inputEl . placeholder = 'Example' ;
} else if ( key === 'domain' )
inputEl . placeholder = 'example.com' ;
labelEl . appendChild ( document . createTextNode ( ' ' + key ) ) ;
add _sitesEl . appendChild ( labelEl ) ;
}
var add _options = {
useragent : useragent _options ,
referer : referer _options
} ;
for ( let key in add _options ) {
labelEl = document . createElement ( 'label' ) ;
labelEl . appendChild ( document . createTextNode ( key + ' ' ) ) ;
inputEl = document . createElement ( 'select' ) ;
inputEl . dataset . key = key ;
labelEl . appendChild ( inputEl ) ;
for ( let elem of add _options [ key ] ) {
let option = document . createElement ( "option" ) ;
option . value = elem ;
option . text = elem ;
inputEl . appendChild ( option ) ;
}
add _sitesEl . appendChild ( labelEl ) ;
}
// list of custom sites
var custom _sitesEl = document . getElementById ( 'custom_sites' ) ;
custom _sitesEl . innerHTML = '' ;
labelEl = document . createElement ( 'label' ) ;
var selectEl = document . createElement ( 'select' ) ;
selectEl . id = 'sites' ;
selectEl . size = 6 ;
var optionEl ;
perm _origins = [ ] ;
for ( let key in sites _custom ) {
optionEl = document . createElement ( 'option' ) ;
let domain = sites _custom [ key ] [ 'domain' ] ;
perm _origins . push ( '*://*.' + domain + '/*' ) ;
let isDefaultSite = defaultSites _domains . includes ( domain ) ;
optionEl . text = isDefaultSite ? '*' : '' ;
optionEl . text += key + ': ' + domain +
( sites _custom [ key ] [ 'googlebot' ] > 0 ? ' | googlebot' : '' ) +
( sites _custom [ key ] [ 'allow_cookies' ] > 0 ? ' | allow_cookies' : '' ) +
( sites _custom [ key ] [ 'block_javascript' ] > 0 ? ' | block javascript' : '' ) +
( sites _custom [ key ] [ 'block_javascript_ext' ] > 0 ? ' | block javascript ext' : '' ) +
( sites _custom [ key ] [ 'block_regex' ] ? ' | block regex' : '' ) +
( sites _custom [ key ] [ 'useragent' ] ? ' | useragent: ' + sites _custom [ key ] [ 'useragent' ] : '' ) +
( sites _custom [ key ] [ 'referer' ] ? ' | referer: ' + sites _custom [ key ] [ 'referer' ] : '' ) ;
optionEl . value = key ;
selectEl . add ( optionEl ) ;
}
labelEl . appendChild ( selectEl ) ;
custom _sitesEl . appendChild ( labelEl ) ;
var perm _custom = document . getElementById ( 'perm-custom' ) ;
ext _api . permissions . contains ( {
origins : [ "<all_urls>" ]
origins : perm _origins
} , function ( result ) {
if ( result ) {
custom _enabled . innerText = 'YES' ;
} else {
custom _enabled . innerText = 'NO' ;
}
if ( result ) {
perm _custom . innerText = 'YES' ;
} else {
perm _custom . innerText = 'NO' ;
}
} ) ;
} ) ;
var custom _enabled = document . getElementById ( 'custom-enabled' ) ;
ext _api . permissions . contains ( {
origins : [ "<all_urls>" ]
} , function ( result ) {
if ( result ) {
custom _enabled . innerText = 'YES' ;
} else {
custom _enabled . innerText = 'NO' ;
}
} ) ;
}
document . addEventListener ( 'DOMContentLoaded' , renderOptions ) ;
@ -345,4 +347,4 @@ document.getElementById('add').addEventListener('click', add_options);
document . getElementById ( 'delete' ) . addEventListener ( 'click' , delete _options ) ;
document . getElementById ( 'edit' ) . addEventListener ( 'click' , edit _options ) ;
document . getElementById ( 'perm_request' ) . addEventListener ( 'click' , request _permissions ) ;
document . getElementById ( 'perm_remove' ) . addEventListener ( 'click' , remove _permissions ) ;
document . getElementById ( 'perm_remove' ) . addEventListener ( 'click' , remove _permissions ) ;