Skip to content

Commit e337add

Browse files
committed
初始化 chrome extension 相关逻辑
1 parent 9eb9fa1 commit e337add

File tree

8 files changed

+154
-6
lines changed

8 files changed

+154
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ node_modules/
77
dist/
88

99
.DS_Store
10+
11+
extension/dist

calcit.cirru

Lines changed: 35 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compact.cirru

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,22 @@
131131
"\"&:focus-within" $ {} (:height "\"260px")
132132
|submit-message! $ %{} :CodeEntry (:doc |)
133133
:code $ quote
134-
defn submit-message! (cursor state prompt-test d!) (hint-fn async)
134+
defn submit-message! (cursor state prompt-text d!) (hint-fn async)
135135
if-let
136136
abort $ deref *abort-control
137-
do (js/console.log "\"Aborting prev") (.!abort abort)
137+
do (js/console.warn "\"Aborting prev") (.!abort abort)
138138
d! cursor $ -> state (assoc :answer nil) (assoc :loading? true)
139139
let
140+
selected $ w-js-log
141+
js-await $ get-selected
142+
content $ .replace prompt-text "\"{{selected}}" (or selected "\"<未找到内容>")
140143
result $ js-await
141144
.!post axios
142145
str "\"https://sf.chenyong.life/v1beta/models/" (pick-model) "\":streamGenerateContent"
143146
js-object $ :contents
144147
js-array $ js-object
145148
:parts $ js-array
146-
js-object $ :text prompt-test
149+
js-object $ :text content
147150
js-object
148151
:params $ js-object
149152
:key $ get-gemini-key!
@@ -189,6 +192,7 @@
189192
"\"axios" :default axios
190193
respo-md.comp.md :refer $ comp-md-block style-code-block
191194
respo-ui.comp :refer $ comp-copy comp-close
195+
"\"../extension/get-selected" :refer $ get-selected
192196
|app.config $ %{} :FileEntry
193197
:defs $ {}
194198
|dev? $ %{} :CodeEntry (:doc |)

extension/content.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Listen for messages
2+
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
3+
// If the received message has the expected format...
4+
console.log("[Side Message] msg", msg)
5+
if (msg.get === 'selected') {
6+
// Call the specified callback, passing
7+
// the web-page's DOM content as argument
8+
sendResponse(getSelectedText());
9+
}
10+
});
11+
12+
let getSelectedText = () => {
13+
if (window.getSelection) {
14+
// 标准浏览器
15+
return window.getSelection().toString();
16+
} else if (document.selection) {
17+
// IE 浏览器
18+
return document.selection.createRange().text;
19+
} else {
20+
return "<未获取到内容>";
21+
}
22+
}
23+
24+
console.log("[Side Message] prepared content script")

extension/get-selected.mjs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
export let get_selected = () => {
3+
return new Promise((resolve, reject) => {
4+
if (chrome?.runtime?.id == null) {
5+
console.warn("not chrome extension runtime...")
6+
resolve(null)
7+
return
8+
}
9+
console.log("calling content script...")
10+
chrome.tabs.query({active: true, currentWindow: true})
11+
.then(x => {
12+
let activeTab = x[0]
13+
if (activeTab) {
14+
let id = activeTab.id
15+
chrome.tabs.sendMessage(id, {get: 'selected'}, function(response) {
16+
// 接收来自 content.js 的返回数据
17+
// console.info('Content script returned: ' + response);
18+
resolve(response)
19+
});
20+
21+
} else {
22+
reject("found not active tab")
23+
}
24+
}).catch((error) => {
25+
console.error("Error", error)
26+
})
27+
})
28+
}
29+
30+
31+
// setTimeout(()=>{
32+
// chrome.tabs.query({active: true, currentWindow: true}).then(x => {
33+
// let activeTab = x[0]
34+
// if (activeTab) {
35+
// let id = activeTab.id
36+
// chrome.tabs.sendMessage(id, {get: 'selected'}, function(response) {
37+
// // 接收来自 content.js 的返回数据
38+
// console.info('Content script returned: ' + response);
39+
// });
40+
41+
// } else {
42+
// throw Error("no active tab found")
43+
// }
44+
// })
45+
// }, 2000)
46+
47+
48+
// chrome.scripting.executeScript({
49+
// target: { tabId: 1201634844 },
50+
// function: () => { console.log(document.body.innerText) }
51+
// });
52+
53+
54+
// chrome.tabs.sendMessage(1201634844, {get: 'selected'}, function(response) {
55+
// // 接收来自 content.js 的返回数据
56+
// console.info('Content script returned: ' + response.message);
57+
// });
58+

extension/manifest.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
{
3+
"manifest_version": 3,
4+
"name": "Side message",
5+
"version": "1.0",
6+
"description": "Shows how to display the same side panel on every site using the Side Panel API.",
7+
"background": {
8+
"service_worker": "service-worker.js"
9+
},
10+
"action": {
11+
"default_title": "Click to open panel"
12+
},
13+
"content_scripts": [{
14+
"matches": ["<all_urls>"],
15+
"match_origin_as_fallback": true,
16+
"js": ["content.js"]
17+
}],
18+
"side_panel": {
19+
"default_path": "dist/index.html"
20+
},
21+
"permissions": ["sidePanel", "scripting", "activeTab", "tabs"]
22+
}

extension/service-worker.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
chrome.runtime.onInstalled.addListener(() => {
2+
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true });
3+
});

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
"bottom-tip": "^0.1.5",
1111
"vite": "^5.3.5"
1212
},
13+
"scripts": {
14+
"build": "yarn vite build --base ./ && rm -rfv extension/dist && cp -vr dist extension/"
15+
},
1316
"version": "0.0.1"
1417
}

0 commit comments

Comments
 (0)