mirror of
https://github.com/Kenshin/simpread.git
synced 2026-02-01 15:41:29 +00:00
Save local/remote websites_list.json to chrome storage logic.
This commit is contained in:
parent
ee33f67922
commit
cf6b0188e5
@ -2,6 +2,17 @@
|
||||
import local from 'local';
|
||||
import { storage, GetNewsites } from 'storage';
|
||||
|
||||
/**
|
||||
* Save local/remote website_list.json to chrome storage
|
||||
*/
|
||||
storage.Get( function() {
|
||||
if ( local.Firstload() ) {
|
||||
storage.GetNewsites( "local" );
|
||||
} else if( !local.Count() ) {
|
||||
storage.GetNewsites( "remote" );
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Create context menus
|
||||
*/
|
||||
@ -40,18 +51,13 @@ function getCurTab( callback ) {
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// load new sites
|
||||
if ( local.Firstload() ) {
|
||||
storage.GetNewsites( chrome.extension.getURL( "website_list.json" ));
|
||||
}
|
||||
|
||||
const ismaxcount = local.Count();
|
||||
console.log( "count = ",local.curcount, ismaxcount )
|
||||
|
||||
/*
|
||||
const firstload = local.Firstload();
|
||||
console.log( "firstload = ",firstload )
|
||||
|
||||
const ismaxcount = local.Count();
|
||||
console.log( "count = ",local.curcount, ismaxcount )
|
||||
|
||||
GetNewsites( chrome.extension.getURL( "website_list.json" )).then( result => {
|
||||
console.log( result )
|
||||
});
|
||||
|
||||
@ -16,7 +16,9 @@ const mode = {
|
||||
read : "read",
|
||||
option : "option",
|
||||
},
|
||||
name = "simpread";
|
||||
name = "simpread",
|
||||
remote = "http://ojec5ddd5.bkt.clouddn.com/website_list.json",
|
||||
local = chrome.extension.getURL( "website_list.json" );
|
||||
|
||||
let site = {
|
||||
name : "", // only read mode
|
||||
@ -48,6 +50,7 @@ let site = {
|
||||
simpread = {
|
||||
focus,
|
||||
read,
|
||||
sites : [],
|
||||
option : {},
|
||||
},
|
||||
origin = {};
|
||||
@ -135,14 +138,19 @@ class Storage {
|
||||
*
|
||||
* @param {string} url, e.g. chrome-extension://xxxx/website_list.json or http://xxxx.xx/website_list.json
|
||||
*/
|
||||
async GetNewsites( url ) {
|
||||
async GetNewsites( type ) {
|
||||
const url = type === "remote" ? remote : local;
|
||||
const response = await fetch( url );
|
||||
const sites = await response.json();
|
||||
simpread.read.sites.push( formatSites( sites ));
|
||||
chrome.storage.local.set( { [name] : simpread }, function() {
|
||||
console.log( "save chrome storage [read] success!", simpread );
|
||||
origin = clone( simpread );
|
||||
});
|
||||
const len = simpread.sites.length;
|
||||
if ( len == 0 ) {
|
||||
simpread.sites = formatSites( sites );
|
||||
} else {
|
||||
addsites( formatSites( sites ));
|
||||
}
|
||||
if ( len == 0 || len != simpread.sites.length ) {
|
||||
save();
|
||||
}
|
||||
//const sites = await fetchJSON( url );
|
||||
//await this.SetNewsites( sites );
|
||||
}
|
||||
@ -210,6 +218,33 @@ function formatSites( result ) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new sites to old sites
|
||||
*
|
||||
* @param {array} new sites from local or remote
|
||||
*/
|
||||
function addsites( sites ) {
|
||||
const old = new Map( simpread.sites );
|
||||
const urls = [ ...old.keys() ];
|
||||
let news = [];
|
||||
for ( const site of sites ) {
|
||||
if ( !urls.includes( site[0] ) ) {
|
||||
news.push( site[0], site[1] );
|
||||
}
|
||||
}
|
||||
if ( news.length > 0 ) simpread.sites.push( news );
|
||||
}
|
||||
|
||||
/**
|
||||
* Call chrome storage set
|
||||
*/
|
||||
function save() {
|
||||
chrome.storage.local.set( { [name] : simpread }, function() {
|
||||
console.log( "chrome storage save success!", simpread );
|
||||
origin = clone( simpread );
|
||||
});
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* Fetch local/remote JSON usage async
|
||||
*
|
||||
* @param {string} url, e.g. chrome-extension://xxxx/website_list.json or http://xxxx.xx/website_list.json
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user