forked from Kenshin/simpread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.js
More file actions
98 lines (89 loc) · 2.94 KB
/
runtime.js
File metadata and controls
98 lines (89 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
console.log( "=== simpread runtime load ===" )
import nanoid from 'nanoid';
import {browser} from 'browser';
import {storage, Clone} from 'storage';
/**
* Generate ID
*
* @param {string} generate id, include: user id( uuid v4 ), plugin id( like t.co/cKPFh3Qsh4 )
*/
function generateID( type ) {
if ( type == "user" ) {
const random = "0123456789abcdefghijklmnopqrstuvwxyz",
first = nanoid( random, 8 ),
second = nanoid( random, 4 ),
third = nanoid( random, 4 ),
fourth = nanoid( random, 4 ),
fifth = nanoid( random, 12 );
return `${first}-${second}-${third}-${fourth}-${fifth}`;
} else if ( type == "plugin" ) {
return nanoid( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 10 );
}
}
/**
* Install plugin
*
* @param {string} id e.g. kw36BtjGu0
* @param {string} url
*/
function install( id, url, callback ) {
url = id ? `http://simpread.ksria.cn/plugins/srplug/${id}.srplug` : url;
url = url + `?${+new Date()}`;
console.log( "install url is", url )
$.ajax({ url, dataType: "json" })
.done( result => callback( result, undefined ))
.fail( () => callback( undefined, "error" ));
}
/**
* Execute
*
* @param {string} state, include: read_start, read_loading, read_complete, read_end
* @param {string} site name, inlcude: "", "xxx", "xxx, yyy"
* @param {object} plugin object
*/
function exec( state, site, plugin ) {
if ( plugin.enable == false ) return;
if ( plugin.run_at != state ) return;
if ( plugin.site != "" && !plugin.site.split(",").includes( site ) ) return;
console.log( "current plugin is running", plugin )
new Function( func( plugin.script ) )();
plugin.style != "" && addStyle( plugin.style );
}
/**
* Contact (function(){})() string
*
* @param {string} source
*/
function func( source ) {
window.Notify = Notify;
window.browser = browser;
window.current = Clone( storage.pr.current );
window.read = Clone( storage.read );
return `( function ( $$version, $title, $desc, $content, $footer, $process, $toc, Notify, browser, $$current, $$read ) {
${ source }
})( "0.0.1", $( "sr-rd-title" ), $( "sr-rd-desc" ), $( "sr-rd-content" ), $( "sr-rd-footer" ), $( "read-process" ), $( "toc" ), Notify, browser, current, read );`
}
/**
* Add style
*
* @param {string} add css to head
*/
function addStyle( str ) {
$( "head" ).append(`<style type="text/css">${str}</style>`);
}
/**
* Test Plugin
*
* @param {func} style func
* @param {func} plugin func
*/
function testPlugin( style, plugin ) {
style && addStyle( style() );
plugin && plugin( "0.0.1", $( "sr-rd-title" ), $( "sr-rd-desc" ), $( "sr-rd-content" ), $( "sr-rd-footer" ), $( "read-process" ), $( "toc" ), Notify, browser, storage.pr.current, storage.read );
}
window.simpread = { testPlugin };
export {
install as Install,
exec as Exec,
generateID as ID,
}