console.log( "===== simpread option labs load =====" ) import {storage} from 'storage'; import * as run from 'runtime'; import * as ss from 'stylesheet'; import {browser} from 'browser'; import * as msg from 'message'; import * as watch from 'watch'; import Button from 'button'; class Card extends React.Component { static defaultProps = { plugin : {}, }; static propTypes = { plugin : React.PropTypes.object, }; update() { run.Install( this.props.plugin.id, undefined, result => { if ( result ) { if ( this.props.plugin.version != result.version ) { storage.plugins[this.props.plugin.id] = result; this.props.onChange( "update" ); } else { new Notify().Render( "当前插件为最新版,无需更新。" ); } } else new Notify().Render( 2, "更新失败,请稍后再试。" ); }); } delete() { new Notify().Render({ mode:"snackbar", content: "是否删除当前插件?", action: "确认", cancel: "取消", callback: type => { if ( type == "cancel" ) return; delete storage.plugins[ this.props.plugin.id ]; storage.option.plugins = Object.keys( storage.plugins ); this.props.onChange( "delete" ); }}); } enable() { this.props.plugin.enable = this.props.plugin.enable == undefined || this.props.plugin.enable ? false : true; this.props.onChange( "enable" ); } addmore() { browser.runtime.sendMessage( msg.Add( msg.MESSAGE_ACTION.new_tab, { url: "https://simpread.ksria.cn/plugins/details/" + this.props.plugin.id })); } render() { const enable = this.props.plugin.enable == undefined || this.props.plugin.enable ? true : false; return ( by { this.props.plugin.user.name } { this.props.plugin.name } `} waves="md-waves-effect md-waves-button" onClick={ ()=>this.enable() } /> this.delete() } /> this.update() } /> this.addmore() } /> ) } } class Cards extends React.Component { static defaultProps = { plugins : [], }; static propTypes = { plugins : React.PropTypes.array, onChange : React.PropTypes.func, }; render() { const card = this.props.plugins.length > 0 ? this.props.plugins.map( ( item, idx ) => { return ( this.props.onChange(t)} /> ) }) : 没有任何插件,点击打开「插件中心」添加。; return ( { card } ) } } export default class PluginsOpt extends React.Component { state = { plugins: [] }; writecb() { watch.SendMessage( "option", true ); } install() { const id = decodeURIComponent( location.hash ).replace( "#plugins?install=", "" ); run.Install( id, undefined, result => { if ( result ) { const add = () => { storage.plugins[result.id] = result; storage.Plugins( result => { new Notify().Render( "当前插件已安装成功,2 秒后自动刷新当前页面。" ); setTimeout( ()=> { location.href = location.origin + location.pathname + "#plugins"; location.reload(); }, 2000 ); }, storage.plugins ); }; if ( !storage.option.plugins.includes( result.id ) ) { add(); storage.option.plugins.push( result.id ); storage.Write( this.writecb ); } else if ( storage.plugins[result.id].version != result.version ) { new Notify().Render({ content: "本地版本与安装版本不一致,是否安装新版本?", action: "安装", cancel: "取消", callback: type => { type == "action" && add(); }}); } else { new Notify().Render({ content: "本地版本与安装版本一致,是否重新安装?", action: "安装", cancel: "取消", callback: type => { type == "action" && add(); }}); } } else new Notify().Render( 2, id + " 获取失败,请稍后再试。" ); }); } clear() { new Notify().Render({ mode: "snackbar", content: "是否清除本地全部插件?", action: "是的", cancel: "取消", callback: type => { if ( type == "action" ) { storage.option.plugins = []; storage.Write( this.writecb ); storage.Plugins( () => { new Notify().Render( "snackbar", "清除成功,此页面需刷新后才能生效!", "刷新 ", ()=>{ location.href = location.origin + location.pathname + "#plugins"; location.reload(); }); }, {} ); } }}); } addmore() { browser.runtime.sendMessage( msg.Add( msg.MESSAGE_ACTION.new_tab, { url: "https://simpread.ksria.cn/plugins" })); } update() { let is_update = false, count = 0; storage.option.plugins.forEach( id => { run.Install( id, undefined, result => { if ( !result ) { new Notify().Render( 2, id + "获取失败,请稍后再试。" ); return; } count++; if ( storage.plugins[id].version != result.version ) { storage.plugins[result.id] = result; is_update = true; } count == storage.option.plugins.length && complete(); }); }); const complete = () => { if ( is_update ) { storage.Plugins( result => { new Notify().Render( "本地插件已全部更新完毕。" ); this.setState({ plugins: Object.values( storage.plugins ) }); }, storage.plugins ); } else { new Notify().Render( "无任何可用更新。" ); } } } import() { if ( storage.option.plugins.length == 0 ) { new Notify().Render( "当前配置文件没有任何插件。" ); return; } new Notify().Render({ mode:"snackbar", content: "导入意味着从配置文件覆盖当前的插件!", action: "确认", cancel: "取消", callback: type => { if ( type == "cancel" ) return; let count = 0; storage.option.plugins.forEach( id => { run.Install( id, undefined, result => { if ( !result ) { new Notify().Render( 2, id + "获取失败,请稍后再试。" ); return; } count++; storage.plugins[result.id] = result; count == storage.option.plugins.length && complete(); }); }); }}); const complete = () => { storage.Plugins( result => { storage.option.plugins = Object.keys( storage.plugins ); storage.Write( () => { new Notify().Render( "已从配置文件导入完毕。" ); this.setState({ plugins: Object.values( storage.plugins ) }); }); }, storage.plugins ); } } onChange( type ) { storage.Write( this.writecb ); storage.Plugins( () => { type == "update" && new Notify().Render( "当前插件已更新成功。" ); type == "delete" && new Notify().Render( "当前插件已删除成功。" ); type == "enable" && new Notify().Render( "当前插件已更改成功。" ); this.setState({ plugins: Object.values( storage.plugins ) }); }, storage.plugins ); } componentWillMount() { $( "head" ).append( '' ); $( "head" ).append( '' ); storage.Plugins( () => { decodeURIComponent( location.href ).includes( "#plugins?install=" ) && this.install(); this.setState({ plugins: Object.values( storage.plugins ) }); }); } render() { return ( 管理 this.import() } /> `} color="#fff" backgroundColor="#00BCD4" waves="md-waves-effect md-waves-button" onClick={ ()=>this.addmore() } /> this.update() } /> this.clear() } /> 已安装 this.onChange(t) } /> ) } }