Javascript Google Chrome扩展获取选定文本有时为空?

Javascript Google Chrome扩展获取选定文本有时为空?,javascript,google-chrome,google-chrome-extension,google-chrome-devtools,google-chrome-app,Javascript,Google Chrome,Google Chrome Extension,Google Chrome Devtools,Google Chrome App,我正在开发小小的chrome扩展 我无法理解getSelection在chrome扩展中是如何工作的 有时所选文本加载,有时为空。为什么 这是我的清单文件: { "background": { "scripts": [ "js/pop.js" ] }, "browser_action": { "default_icon": "icons/icon.png", "default_popup": "popup.html" }, "con

我正在开发小小的chrome扩展

我无法理解getSelection在chrome扩展中是如何工作的

有时所选文本加载,有时为空。为什么

这是我的清单文件:

{
  "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"
  ]
}
这是我的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>

jquerymobile:主题下载

我认为您的问题在于获取所选文本的方式

而是使用:

var selectedText = window.getSelection().toString();
请尝试以下其他代码:

var-focused=document.activeElement;
var选择文本;
如果(聚焦){
试一试{
selectedText=focused.value.substring(
聚焦。选择开始,聚焦。选择结束);
}捕捉(错误){
}
}
如果(selectedText==未定义){
var sel=window.getSelection();
selectedText=sel.toString();
}

chrome.tabs.sendMessage默认情况下将消息发送到页面的所有帧。尝试根据指定
frameId:0
var selectedText = window.getSelection().toString();