谷歌Chrome扩展获取所选文本有时为空

Google Chrome Extension Getting Selected Text Sometimes Empty?

本文关键字:文本 Chrome 扩展 获取 谷歌      更新时间:2023-09-26

我正在开发小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>

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

改为使用:

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