-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
154 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ node_modules/ | |
dist/ | ||
|
||
.DS_Store | ||
|
||
extension/dist |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// }); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
chrome.runtime.onInstalled.addListener(() => { | ||
chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters