-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebean.ts
71 lines (65 loc) · 2.6 KB
/
firebean.ts
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
import {FireBean as FireBeanTypes ,Omit} from '@foxzilla/fireblog';
declare var localStorage:any;
declare var window:any;
declare var location:any;
declare var setTimeout:any;
export const DefaultData ={
_firebean:'1',
_version :'0.0.1-alpha.26',
_close :FireBeanTypes.CloseType.justClose,
_redirect:'/',
};
export const Actions:{
set_storage: (data:FireBeanTypes.SetStorageData)=>void,
remove_storage: (data:FireBeanTypes.RemoveStorageData)=>void,
go_article: (data:FireBeanTypes.GoArticleData)=>void,
go_comment: (data:FireBeanTypes.GoCommentData)=>void,
go_user: (data:FireBeanTypes.GoUserData)=>void,
} ={
set_storage(data){
localStorage.setItem(data.key,data.value);
},
remove_storage(data){
localStorage.removeItem(data.key);
},
go_article(data){},
go_comment(data){},
go_user(data){},
};
export function stringify(inputData:Omit<FireBeanTypes.SetStorageData,'_version'|'_firebean'> ,baseUrl?:string):string;
export function stringify(inputData:Omit<FireBeanTypes.RemoveStorageData,'_version'|'_firebean'> ,baseUrl?:string):string;
export function stringify(inputData:Omit<FireBeanTypes.GoArticleData,'_version'|'_firebean'> ,baseUrl?:string):string;
export function stringify(inputData:Omit<FireBeanTypes.GoCommentData,'_version'|'_firebean'> ,baseUrl?:string):string;
export function stringify(inputData:Omit<FireBeanTypes.GoUserData,'_version'|'_firebean'> ,baseUrl?:string):string;
export function stringify(inputData:any ,baseUrl =''):string{
return `${baseUrl}/_firebean?${
Object.entries(Object.assign({},DefaultData,inputData))
.map(
([key,value]) =>`${encodeURIComponent(key)}=${encodeURIComponent(value.toString())}`
)
.join('&')
}`;
}
export function exec(input:FireBeanTypes.Data|string =location.href){
var data:FireBeanTypes.Data =typeof input ==='string' ?parse(input) :input;
//@ts-ignore
Actions[data._type](data);
if(data._close ===FireBeanTypes.CloseType.justClose){
window.close();
}else if(data._close ===FireBeanTypes.CloseType.onlyBlank){
if(window.opener) window.close();
};
setTimeout(
()=>window.open(data._redirect,'_self'),
0,
);
}
export function parse(url:string):FireBeanTypes.Data{
var queryString:string =url.split('?').pop()!.split('#').shift()!;
var data:any ={};
for(let item of queryString.split('&')){
let [key,value] =item.split('=');
data[decodeURIComponent(key)] =decodeURIComponent(value);
};
return Object.assign({},DefaultData,data) as FireBeanTypes.Data;
}