gpt4 book ai didi

javascript - Google Chrome 扩展程序有时会选择空的文本?

转载 作者:行者123 更新时间:2023-11-30 16:27:23 25 4
gpt4 key购买 nike

我正在开发小的 chrome 扩展程序。

我不知道 getSelection 在 chrome 扩展中是如何工作的。

有时选中的文本会加载,有时是空的。为什么?!

这是我的 list 文件:

{
"background": {
"scripts": [
"js/pop.js"
]
},
"browser_action": {
"default_icon": "icons/icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"js": [
"js/contentscript.js",
"js/lib/jquery-2.1.4.min.js",
"js/lib/hi5.js"
],
"matches": [
"http://*/*",
"https://*/*",
"file://*",
"<all_urls>"
],
"run_at": "document_end",
"all_frames": true
}
],
"icons": {
"16": "icons/icon16.png",
"24": "icons/icon24.png",
"32": "icons/icon32.png",
"48": "icons/icon48.png",
"64": "icons/icon64.png",
"96": "icons/icon96.png",
"128": "icons/icon128.png"
},
"commands": {
"_execute_browser_action": {
"suggested_key": {
"chromeos": "Alt+D"
}
}
},
"name": "App",
"manifest_version": 2,
"permissions": [
"tabs",
"contentSettings",
"management",
"contextMenus",
"http://*/",
"https://*/",
"<all_urls>",
"storage",
"unlimitedStorage"
],
"update_url": "https://clients2.google.com/service/update2/crx",
"version": "1.0",
"web_accessible_resources": [
"js/lib/jquery-2.1.4.min.js"
]
}

这是我的内容脚本:

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {


if (request.method == "getSelection") {

var selectedText = window.getSelection().toString();
sendResponse({data: selectedText});
}
}
);

这是我的 pop.js:

document.addEventListener('DOMContentLoaded', function () {

chrome.tabs.query({active: true, windowId: chrome.windows.WINDOW_ID_CURRENT},
function (tab) {

chrome.tabs.sendMessage(tab[0].id, {method: "getSelection"},
function (response) {

var selectedTex = response.data;
if (selectedTex.length > 1) {

$("#search-basic").val(selectedTex);

}
});
});
});

这是我的 popUp.html

<html>
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery Mobile: Theme Download</title>

<link rel="stylesheet" href="themes/deneme.min.css"/>
<link rel="stylesheet" href="themes/jquery.mobile.icons.min.css"/>
<link rel="stylesheet" href="./css/jquery.mobile-1.4.5.min.css"/>

<script type="text/javascript" src="./js/lib/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="./js/lib/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" src="./js/pop.js"></script>
</head>

<body>

<input type="search" name="search" id="search-basic" value=""/>
</body>
</html>

最佳答案

我假设你的问题是你用来获取所选文本的方式。

代替使用:

var selectedText = window.getSelection().toString();

试试这个其他代码:

var focused = document.activeElement;
var selectedText;
if (focused) {
try {
selectedText = focused.value.substring(
focused.selectionStart, focused.selectionEnd);
} catch (err) {
}
}
if (selectedText == undefined) {
var sel = window.getSelection();
selectedText = sel.toString();
}

关于javascript - Google Chrome 扩展程序有时会选择空的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33904816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com