📦 hyoban / inbox-note-ext

📄 App.tsx · 67 lines
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
67import { useState } from "react";

function onSaveButtonClick(content: string) {
  chrome.storage.local.get(["token"], function (result) {
    if (content === undefined || content.length === 0) {
      alert("请输入有效内容");
    } else if (result.token === undefined) {
      alert("请先在设置页面填入 API");
    } else {
      fetch(result.token, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          content,
        }),
      })
        .then((response) => response.json())
        .then((data) => {
          if (data.code === 0) {
            alert(data.msg);
            window.close();
          } else {
            alert(`上传失败,${data.msg}`);
          }
        })
        .catch((error) => {
          alert(error);
        });
    }
  });
}

function App() {
  const [content, setContent] = useState("");

  return (
    <div className="w-96 h-96 p-3 flex flex-col bg-gray-200">
      <label htmlFor="content" className="mb-3 inline-block text-lg">
        笔记:
      </label>
      <textarea
        id="content"
        rows={10}
        placeholder="请在此输入笔记"
        className="mb-3 appearance-none block w-full bg-white text-gray-700 border border-gray-400 shadow-inner rounded-md py-3 px-4 leading-tight focus:outline-none  focus:border-gray-500"
        value={content}
        onChange={(e) => {
          setContent(e.target.value);
        }}
      />
      <button
        type="submit"
        className="self-end mr-2 w-24 py-2 px-4 appearance-none bg-gray-200 text-gray-900 shadow-sm border border-gray-400 rounded-md"
        onClick={() => {
          onSaveButtonClick(content);
        }}
      >
        发送
      </button>
    </div>
  );
}

export default App;