Skip to content

Commit 13ae270

Browse files
committed
feat: basic framework
0 parents  commit 13ae270

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Page AI
2+
3+
Answer questions based on the content of the page.

assets/logo.png

153 KB
Loading

manifest.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"manifest_version": 3,
3+
"name": "Page AI",
4+
"description": "页面 AI",
5+
"version": "1.0",
6+
"action": {
7+
"default_popup": "popup/popup.html",
8+
"default_icon": "assets/logo.png"
9+
},
10+
"host_permissions": ["https://*/"]
11+
}

popup/popup.css

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
* {
2+
padding: 0;
3+
margin: 0;
4+
}
5+
6+
*::-webkit-scrollbar {
7+
width: 3px;
8+
}
9+
10+
*::-webkit-scrollbar-thumb {
11+
border-radius: 10px;
12+
background-color: #b2bbbe;
13+
}
14+
15+
*::-webkit-scrollbar-track {
16+
border-radius: 10px;
17+
}
18+
19+
body {
20+
width: 300px;
21+
background-color: #eceff7;
22+
}
23+
24+
#app {
25+
padding: 5px 10px 10px;
26+
}
27+
28+
.title {
29+
text-align: center;
30+
}
31+
32+
.box .search {
33+
display: flex;
34+
justify-content: center;
35+
align-items: center;
36+
margin-top: 10px;
37+
border: 1px solid rgb(210, 210, 210);
38+
border-radius: 10px;
39+
background-color: #ffff;
40+
}
41+
42+
.box .search .search-ipt {
43+
width: 220px;
44+
height: 38px;
45+
border: none;
46+
}
47+
48+
.box .search .search-ipt:focus-visible {
49+
outline: none;
50+
}
51+
52+
.box .search .search-btn {
53+
width: 30px;
54+
height: 30px;
55+
border: none;
56+
border-radius: 5px;
57+
color: #ffff;
58+
background-color: #c5c5c5;
59+
}
60+
61+
.box .search .search-btn:hover {
62+
background-color: #bbbbbb;
63+
}
64+
65+
/* message list */
66+
67+
.box .message-list {
68+
margin-top: 10px;
69+
max-height: 390px;
70+
overflow-y: scroll;
71+
word-wrap: break-word;
72+
}
73+
74+
.box .message-list .item {
75+
margin: 12px 0;
76+
padding: 6px;
77+
border-radius: 6px;
78+
background-color: #ffff;
79+
}

popup/popup.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>页面 AI</title>
5+
<link rel="stylesheet" href="./popup.css" />
6+
</head>
7+
8+
<body>
9+
<div id="app">
10+
<h1 class="title">页面 AI</h1>
11+
12+
<div class="box">
13+
<div class="search">
14+
<input type="text" class="search-ipt" placeholder="" />
15+
<button class="search-btn">
16+
<svg width="24" height="24" viewBox="0 0 24 24" fill="none">
17+
<path
18+
d="M7 11L12 6L17 11M12 18V7"
19+
stroke="currentColor"
20+
stroke-width="2"
21+
stroke-linecap="round"
22+
stroke-linejoin="round"
23+
></path>
24+
</svg>
25+
</button>
26+
</div>
27+
28+
<div class="message-list"></div>
29+
</div>
30+
</div>
31+
32+
<script src="./popup.js"></script>
33+
</body>
34+
</html>

popup/popup.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const API_KEY = ''
2+
3+
const searchInput = document.querySelector('.search-ipt')
4+
const searchBtn = document.querySelector('.search-btn')
5+
6+
const messageList = document.querySelector('.message-list')
7+
8+
searchBtn.addEventListener('click', async () => {
9+
const searchContent = searchInput.value
10+
console.log('input', searchContent)
11+
12+
const div = document.createElement('div')
13+
div.setAttribute('class', 'item')
14+
15+
const aiResponseMessage = await fetchOpenAI(searchContent)
16+
17+
console.log('searchContent', searchContent)
18+
console.log('aiResponseMessage', aiResponseMessage)
19+
20+
div.innerText = aiResponseMessage
21+
messageList.insertBefore(div, messageList.firstElementChild)
22+
})
23+
24+
async function fetchOpenAI(searchContent) {
25+
const bodyOptions = {
26+
model: 'gpt-3.5-turbo',
27+
messages: [
28+
{ role: 'system', content: '你需要根据内容回答用户问题' },
29+
{ role: 'user', content: searchContent }
30+
],
31+
temperature: 0.1
32+
}
33+
34+
try {
35+
const response = await fetch('', {
36+
headers: {
37+
'Content-Type': 'application/json',
38+
Authorization: `Bearer ${API_KEY}`
39+
},
40+
method: 'post',
41+
body: JSON.stringify(bodyOptions)
42+
})
43+
44+
const responseData = await response.json()
45+
46+
const result = responseData.choices[0].message.content
47+
48+
return result
49+
} catch (error) {
50+
console.log(`error: ${error.message}`)
51+
}
52+
}

0 commit comments

Comments
 (0)