-
Notifications
You must be signed in to change notification settings - Fork 174
/
popup.tsx
185 lines (161 loc) · 9.12 KB
/
popup.tsx
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { useState, useEffect } from "react"
import { sendToBackground } from "@plasmohq/messaging"
import type { RequestBody, ResponseBody } from "~background/messages/config"
import short_uid from 'short-uuid';
import "./style.scss"
import { load_data, save_data } from './function';
import browser from 'webextension-polyfill';
function IndexPopup() {
let init: Object={"endpoint":"http://127.0.0.1:8088","password":"","interval":10,"domains":"","uuid":String(short_uid.generate()),"type":"up","keep_live":"","with_storage":1,"blacklist":"google.com", "headers": "","expire_minutes":60*24*365,"webhook_url":""};
const [data, setData] = useState(init);
async function test(action=browser.i18n.getMessage('test'))
{
console.log("request,begin");
if( !data['endpoint'] || !data['password'] || !data['uuid'] || !data['type'] )
{
alert(browser.i18n.getMessage("fullMessagePlease"));
return;
}
if( data['type'] == 'pause' )
{
// alert('暂停状态不能'+action);
alert(browser.i18n.getMessage("actionNotAllowedInPause"));
return;
}
const ret = await sendToBackground<RequestBody, ResponseBody>({name:"config",body:{payload:{...data,no_cache:1}}});
console.log(action+"返回",ret);
if( ret && ret['message'] == 'done' )
{
if( ret['note'] )
alert(ret['note']);
else
alert(action+browser.i18n.getMessage('success'));
}else
{
alert(action+browser.i18n.getMessage('failedCheckInfo'));
}
}
async function save()
{
if( !data['endpoint'] || !data['password'] || !data['uuid'] || !data['type'] )
{
// alert('请填写完整的信息');
alert(browser.i18n.getMessage("fullMessagePlease"));
return;
}
await save_data( "COOKIE_SYNC_SETTING", data );
const ret = await load_data("COOKIE_SYNC_SETTING") ;
console.log( "load", ret );
if( JSON.stringify(ret) == JSON.stringify(data) )
{
// alert('保存成功');
alert(browser.i18n.getMessage("saveSuccess"));
window.close();
}
}
function onChange(name:string, e:(React.ChangeEvent<HTMLInputElement|HTMLTextAreaElement>))
{
// console.log( "e" , name , e.target.value );
setData({...data,[name]:e.target.value??''});
}
function uuid_regen()
{
setData({...data,'uuid':String(short_uid.generate())});
}
function password_gen()
{
setData({...data,'password':String(short_uid.generate())});
}
useEffect(() => {
async function load_config()
{
const ret = await load_data("COOKIE_SYNC_SETTING") ;
if( ret ) setData({...data,...ret});
}
load_config();
},[]);
return <div className="w-128 overflow-x-hidden" style={{"width":"360px"}}>
<div className="form p-5">
<div className="text-line text-gray-600">
<div className="">{browser.i18n.getMessage('workingMode')}</div>
<div className="my-2">
{/*
<Radio.Group onChange={e=>onChange('type',e)} value={data['type']}>
<Radio value={'up'}>上传到服务器</Radio>
<Radio value={'down'}>覆盖到浏览器</Radio>
<Radio value={'pause'}>暂停</Radio>
</Radio.Group>
*/}
<label className="mr-2"><input type="radio" name="type" value="up" checked={data['type'] == 'up'} onChange={e=>onChange('type',e)} /> {browser.i18n.getMessage('upToServer')}</label>
<label className="mr-2"><input type="radio" name="type" value="down" checked={data['type'] == 'down'} onChange={e=>onChange('type',e)} /> {browser.i18n.getMessage('overwriteToBrowser')}
</label>
<label className="mr-2"><input type="radio" name="type" value="pause" checked={data['type'] == 'pause'} onChange={e=>onChange('type',e)} /> {browser.i18n.getMessage('pauseSync')}</label>
</div>
{data['type'] && data['type'] == 'down' && <div className="bg-red-600 text-white p-2 my-2 rounded">
{browser.i18n.getMessage('overwriteModeDesp')}
</div>}
{data['type'] && data['type'] != 'pause' && <>
<div className="">{browser.i18n.getMessage('serverHost')}</div>
<input type="text" className="border-1 my-2 p-2 rounded w-full" placeholder={browser.i18n.getMessage('serverHostPlaceholder')} value={data['endpoint']} onChange={e=>onChange('endpoint',e)} />
<div className="">{browser.i18n.getMessage('uuid')}</div>
<div className="flex flex-row">
<div className="left flex-1">
<input type="text" className="border-1 my-2 p-2 rounded w-full" placeholder={browser.i18n.getMessage('uuidPlaceholder')} value={data['uuid']} onChange={e=>onChange('uuid',e)}/>
</div>
<div className="right">
<button className="p-2 rounded my-2 ml-2" onClick={()=>uuid_regen()}>{browser.i18n.getMessage('reGenerate')}</button>
</div>
</div>
<div className="">{browser.i18n.getMessage('syncPassword')}</div>
<div className="flex flex-row">
<div className="left flex-1">
<input type="text" className="border-1 my-2 p-2 rounded w-full" placeholder={browser.i18n.getMessage('syncPasswordPlaceholder')} value={data['password']} onChange={e=>onChange('password',e)}/>
</div>
<div className="right">
<button className="p-2 rounded my-2 ml-2" onClick={()=>password_gen()}>{browser.i18n.getMessage('generate')}</button>
</div>
</div>
<div className="">{browser.i18n.getMessage('cookieExpireMinutes')}</div>
<input type="number" className="border-1 my-2 p-2 rounded w-full" placeholder={browser.i18n.getMessage('cookieExpireMinutesPlaceholder')} value={data['expire_minutes']||0} onChange={e=>onChange('expire_minutes',e)} />
<div className="">{browser.i18n.getMessage('syncTimeInterval')}</div>
<input type="number" className="border-1 my-2 p-2 rounded w-full" placeholder={browser.i18n.getMessage('syncTimeIntervalPlaceholder')} value={data['interval']} onChange={e=>onChange('interval',e)} />
{data['type'] && data['type'] == 'up' && <>
<div className="">{browser.i18n.getMessage('syncLocalStorageOrNot')}</div>
<div className="my-2 flex flex-row items-center">
{/*
<Radio.Group onChange={e=>onChange('with_storage',e)} value={data['with_storage']}>
<Radio value={1}>是</Radio>
<Radio value={0}>否</Radio>
</Radio.Group>
*/}
<label className="mr-2"><input type="radio" name="with_storage" value="1" checked={data['with_storage'] == 1} onChange={e=>onChange('with_storage',e)} /> {browser.i18n.getMessage('yes')}</label>
<label className="mr-2"><input type="radio" name="with_storage" value="0" checked={data['with_storage'] == 0} onChange={e=>onChange('with_storage',e)} /> {browser.i18n.getMessage('no')}</label>
</div>
<div className="">{browser.i18n.getMessage('requestHeader')}</div>
<textarea className="border-1 my-2 p-2 rounded w-full" style={{"height":"60px"}} placeholder={browser.i18n.getMessage('requestHeaderPlaceholder')} onChange={e=>onChange('headers',e)} value={data['headers']}/>
<div className="">{browser.i18n.getMessage('webhookUrl')}</div>
<input type="text" className="border-1 my-2 p-2 rounded w-full" placeholder={browser.i18n.getMessage('webhookUrlPlaceholder')} value={data['webhook_url']} onChange={e=>onChange('webhook_url',e)} />
<div className="">{browser.i18n.getMessage('syncDomainKeyword')}</div>
<textarea className="border-1 my-2 p-2 rounded w-full" style={{"height":"60px"}} placeholder={browser.i18n.getMessage('syncDomainKeywordPlaceholder')} onChange={e=>onChange('domains',e)} value={data['domains']}/>
<div className="">{browser.i18n.getMessage('syncDomainBlacklist')}</div>
<textarea className="border-1 my-2 p-2 rounded w-full" style={{"height":"60px"}} placeholder={browser.i18n.getMessage('syncDomainBlacklistPlaceholder')} onChange={e=>onChange('blacklist',e)} value={data['blacklist']}/>
<div className="">{browser.i18n.getMessage('cookieKeepLive')}</div>
<textarea className="border-1 my-2 p-2 rounded w-full" style={{"height":"60px"}} placeholder={browser.i18n.getMessage('cookieKeepLivePlaceholder')} onChange={e=>onChange('keep_live',e)} value={data['keep_live']}/>
</>}
</>}
{data['type'] && data['type'] == 'pause' && <>
<div className="bg-blue-400 text-white p-2 my-2 rounded">{browser.i18n.getMessage('keepLiveStop')}</div>
</>}
<div className="flex flex-row justify-between mt-2">
<div className="left text-gray-400">
{data['type'] && data['type'] != 'pause' && <><button className="p-2 rounded hover:bg-blue-100 mr-2" onClick={()=>test(browser.i18n.getMessage('syncManual'))}>{browser.i18n.getMessage('syncManual')}</button><button className="hover:bg-blue-100 p-2 rounded" onClick={()=>test(browser.i18n.getMessage('test'))}>{browser.i18n.getMessage('test')}</button></>}
</div>
<div className="right">
<button className="p-2 rounded" onClick={()=>save()}>{browser.i18n.getMessage('save')}</button>
</div>
</div>
</div>
</div>
</div>
}
export default IndexPopup