@@ -26,7 +26,7 @@ export const queryAll = (selector, context = document) => {
2626 return [ ] . slice . apply ( context . querySelectorAll ( selector ) )
2727}
2828export const noop = ( func , defaultFunc ) => {
29- return typeof func === 'function' ? func : typeof defaultFunc === 'function' ? defaultFunc : ( ) => { }
29+ return typeof func === 'function' ? func : typeof defaultFunc === 'function' ? defaultFunc : ( ) => { }
3030}
3131export const encodeUrlData = ( data ) => {
3232 let body = ''
@@ -75,23 +75,23 @@ export const formatDate = (str, t) => {
7575 const obj = {
7676 yyyyyyyy : t . getFullYear ( ) ,
7777 yy : t . getFullYear ( ) ,
78- MM : t . getMonth ( ) + 1 ,
78+ MM : t . getMonth ( ) + 1 ,
7979 dd : t . getDate ( ) ,
8080 HH : t . getHours ( ) ,
8181 hh : t . getHours ( ) % 12 ,
8282 mm : t . getMinutes ( ) ,
8383 ss : t . getSeconds ( ) ,
8484 ww : '日一二三四五六' . split ( '' ) [ t . getDay ( ) ]
85- } ;
86- return str . replace ( / ( [ a - z ] + ) / ig, function ( $1 ) {
87- return ( obj [ $1 + $1 ] === 0 ? '0' : obj [ $1 + $1 ] ) || ( '0' + obj [ $1 ] ) . slice ( - 2 ) ;
88- } ) ;
85+ }
86+ return str . replace ( / ( [ a - z ] + ) / ig, function ( $1 ) {
87+ return ( obj [ $1 + $1 ] === 0 ? '0' : obj [ $1 + $1 ] ) || ( '0' + obj [ $1 ] ) . slice ( - 2 )
88+ } )
8989}
9090export const insertAfter = ( newElement , targetElement ) => {
9191 const parent = targetElement . parentNode
92- if ( parent . lastChild === targetElement ) {
92+ if ( parent . lastChild === targetElement ) {
9393 parent . appendChild ( newElement )
94- } else {
94+ } else {
9595 parent . insertBefore ( newElement , targetElement . nextSibling )
9696 }
9797}
@@ -106,14 +106,14 @@ export const getUrl = (prefix, link) => {
106106 return prefix + link
107107}
108108export const exec = async ( ...rest ) => {
109- if ( ! rest . length ) return exec . returnValue
110- exec . returnValue = false
111- try {
112- exec . returnValue = typeof rest [ 0 ] === 'function' && await rest [ 0 ] ( ...rest . slice ( 1 ) )
113- } catch ( err ) {
114- console . warn ( err )
115- }
116- return exec . returnValue
109+ if ( ! rest . length ) return exec . returnValue
110+ exec . returnValue = false
111+ try {
112+ exec . returnValue = typeof rest [ 0 ] === 'function' && await rest [ 0 ] ( ...rest . slice ( 1 ) )
113+ } catch ( err ) {
114+ console . warn ( err )
115+ }
116+ return exec . returnValue
117117}
118118export const getLocal = ( key ) => {
119119 return new Promise ( ( resolve ) => {
@@ -128,17 +128,25 @@ export const setWebsite = async (website, options) => {
128128 { } ,
129129 localWebsites instanceof Object ? localWebsites : { }
130130 )
131- const valid = website
132- &&
133- options instanceof Object
134- &&
135- Array . isArray ( options . hosts )
136- &&
137- options . hosts . length
138- &&
139- ( options . selectors || { } ) . body
140- if ( valid ) {
131+ const rules = [
132+ [
133+ website && typeof website === 'string' ,
134+ '第一个参数应该是字符串类型, 并且不能为空'
135+ ] ,
136+ [
137+ Array . isArray ( options . hosts ) && options . hosts . length ,
138+ 'options.hosts应该是数组,并且长度不能为0'
139+ ] ,
140+ [
141+ ( options . selectors || { } ) . body ,
142+ 'options.selectors.body不能为空'
143+ ]
144+ ]
145+ const hit = rules . find ( item => ! item [ 0 ] )
146+ if ( ! hit ) {
141147 options . origin = website
148+ options . timestamp = Date . now ( )
149+ options . hosts = options . hosts . map ( item => item . toString ( ) )
142150 Object . assign (
143151 websites ,
144152 {
@@ -153,6 +161,8 @@ export const setWebsite = async (website, options) => {
153161 chrome . storage . local . set ( {
154162 websites
155163 } )
164+ } else {
165+ hit [ 1 ] && console . warn ( hit [ 1 ] )
156166 }
157167 return websites
158168}
0 commit comments