Google Chrome扩展程序获取所选文本有时会变空?

时间:2015-11-24 22:10:01

标签: 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"
  },
  "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>

1 个答案:

答案 0 :(得分:2)

我认为您的问题是您用来获取所选文本的方式。

改为使用:

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();
  }