Skip to content

Commit

Permalink
初始化 chrome extension 相关逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Aug 8, 2024
1 parent 9eb9fa1 commit e337add
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ node_modules/
dist/

.DS_Store

extension/dist
38 changes: 35 additions & 3 deletions calcit.cirru

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions compact.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,22 @@
"\"&:focus-within" $ {} (:height "\"260px")
|submit-message! $ %{} :CodeEntry (:doc |)
:code $ quote
defn submit-message! (cursor state prompt-test d!) (hint-fn async)
defn submit-message! (cursor state prompt-text d!) (hint-fn async)
if-let
abort $ deref *abort-control
do (js/console.log "\"Aborting prev") (.!abort abort)
do (js/console.warn "\"Aborting prev") (.!abort abort)
d! cursor $ -> state (assoc :answer nil) (assoc :loading? true)
let
selected $ w-js-log
js-await $ get-selected
content $ .replace prompt-text "\"{{selected}}" (or selected "\"<未找到内容>")
result $ js-await
.!post axios
str "\"https://sf.chenyong.life/v1beta/models/" (pick-model) "\":streamGenerateContent"
js-object $ :contents
js-array $ js-object
:parts $ js-array
js-object $ :text prompt-test
js-object $ :text content
js-object
:params $ js-object
:key $ get-gemini-key!
Expand Down Expand Up @@ -189,6 +192,7 @@
"\"axios" :default axios
respo-md.comp.md :refer $ comp-md-block style-code-block
respo-ui.comp :refer $ comp-copy comp-close
"\"../extension/get-selected" :refer $ get-selected
|app.config $ %{} :FileEntry
:defs $ {}
|dev? $ %{} :CodeEntry (:doc |)
Expand Down
24 changes: 24 additions & 0 deletions extension/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Listen for messages
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
// If the received message has the expected format...
console.log("[Side Message] msg", msg)
if (msg.get === 'selected') {
// Call the specified callback, passing
// the web-page's DOM content as argument
sendResponse(getSelectedText());
}
});

let getSelectedText = () => {
if (window.getSelection) {
// 标准浏览器
return window.getSelection().toString();
} else if (document.selection) {
// IE 浏览器
return document.selection.createRange().text;
} else {
return "<未获取到内容>";
}
}

console.log("[Side Message] prepared content script")
58 changes: 58 additions & 0 deletions extension/get-selected.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

export let get_selected = () => {
return new Promise((resolve, reject) => {
if (chrome?.runtime?.id == null) {
console.warn("not chrome extension runtime...")
resolve(null)
return
}
console.log("calling content script...")
chrome.tabs.query({active: true, currentWindow: true})
.then(x => {
let activeTab = x[0]
if (activeTab) {
let id = activeTab.id
chrome.tabs.sendMessage(id, {get: 'selected'}, function(response) {
// 接收来自 content.js 的返回数据
// console.info('Content script returned: ' + response);
resolve(response)
});

} else {
reject("found not active tab")
}
}).catch((error) => {
console.error("Error", error)
})
})
}


// setTimeout(()=>{
// chrome.tabs.query({active: true, currentWindow: true}).then(x => {
// let activeTab = x[0]
// if (activeTab) {
// let id = activeTab.id
// chrome.tabs.sendMessage(id, {get: 'selected'}, function(response) {
// // 接收来自 content.js 的返回数据
// console.info('Content script returned: ' + response);
// });

// } else {
// throw Error("no active tab found")
// }
// })
// }, 2000)


// chrome.scripting.executeScript({
// target: { tabId: 1201634844 },
// function: () => { console.log(document.body.innerText) }
// });


// chrome.tabs.sendMessage(1201634844, {get: 'selected'}, function(response) {
// // 接收来自 content.js 的返回数据
// console.info('Content script returned: ' + response.message);
// });

22 changes: 22 additions & 0 deletions extension/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

{
"manifest_version": 3,
"name": "Side message",
"version": "1.0",
"description": "Shows how to display the same side panel on every site using the Side Panel API.",
"background": {
"service_worker": "service-worker.js"
},
"action": {
"default_title": "Click to open panel"
},
"content_scripts": [{
"matches": ["<all_urls>"],
"match_origin_as_fallback": true,
"js": ["content.js"]
}],
"side_panel": {
"default_path": "dist/index.html"
},
"permissions": ["sidePanel", "scripting", "activeTab", "tabs"]
}
3 changes: 3 additions & 0 deletions extension/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
chrome.runtime.onInstalled.addListener(() => {
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true });
});
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"bottom-tip": "^0.1.5",
"vite": "^5.3.5"
},
"scripts": {
"build": "yarn vite build --base ./ && rm -rfv extension/dist && cp -vr dist extension/"
},
"version": "0.0.1"
}

0 comments on commit e337add

Please sign in to comment.