2

我想制作一个使用弹出窗口的 chrome 扩展:

  1. 选择文本
  2. 单击 chrome 扩展程序图标
  3. 在弹出窗口中获取它(textarea,...)

这个问题已经在这里问过了,但谷歌做了更新,我发现的代码不再工作了......

选择.js

    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if (request.method == "getSelection")
      sendResponse({data: window.getSelection().toString()});
    else
      sendResponse({}); // snub them.
});

popup.html

    <!DOCTYPE html> 
<html>
<head>
<style>
  body { width: 300px; }
  textarea { width: 250px; height: 100px;}
</style>
<script>


function pasteSelection() {
  chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {method: "getSelection"}, function (response) {
      var text = document.getElementById('text'); 
      text.innerHTML = response.data;
    });
  });
}

function getSelectedText(){
   if (window.getSelection){
      var str = window.getSelection();
   }else if (document.getSelection){
      var str = document.getSelection();
   }else {
      var str = document.selection.createRange().text;
   }
   return str;
}

function affichage(){
var sel = getSelectedText();
alert(sel);
}

function addtext() {
    document.form.champ.value = getSelectedText();
}

</script>
</head>
<body>
<form>
<textarea id="text"></textarea>
<button onclick="pasteSelection(); " type="submit">get text</button>
</form>
</body>
</html>

清单.json

 {
 "name": "Selected Text",
 "version": "0.1",
 "description": "Selected Text",
 "options_page": "page_options.html",
 "browser_action": {
   "default_title": "Selected Text",
   "default_icon": "icon.png",
   "default_popup": "popup.html" 
 },
 "permissions": [
   "tabs",
   "chrome://favicon/",
   "http://*/*", 
   "https://*/*"
 ],
 "content_scripts": [
  {
    "matches": ["http://*/*"],
    "js": ["selection.js"],
    "run_at": "document_start",
    "all_frames": true
  }
 ],
 "manifest_version": 2
}

我提前感谢您的帮助:)

4

1 回答 1

1

您的脚本中有多个问题

  • chrome.extension.onRequest 已被弃用,取而代之的是 chrome.extension.onMessage
  • chrome.tabs.sendRequest 已被弃用,取而代之的是 chrome.tabs.sendMessage
  • CSP 不允许<script>在 html 代码中使用内联脚本和标记。
  • Content Script 的 window 对象不同于普通的页面窗口对象。

对代码应用多项更改后,我得到了它的工作

清单.json

消除了清单中不适用的部分

{
 "name": "Selected Text",
 "version": "0.1",
 "description": "Selected Text",
 "browser_action": {
   "default_title": "Selected Text",
   "default_popup": "popup.html" 
 },
 "permissions": [
   "tabs",
   "http://*/*", 
   "https://*/*"
 ],
 "content_scripts": [
  {
    "matches": ["<all_urls>"],
    "js": ["selection.js"],
    "run_at": "document_start",
    "all_frames": true
  }
 ],
 "manifest_version": 2
}

popup.html

确保popup.html遵守 CSP

<!DOCTYPE html>
<html>

    <head>
        <style>
            body {
                width: 300px;
            }
            textarea {
                width: 250px;
                height: 100px;
            }
        </style>
        <script src="popup.js"></script>
    </head>

    <body>
        <textarea id="text"></textarea>
        <button id="submit">get text</button>
    </body>

</html>

popup.js

用于选择当前选项卡并发送消息和更新 DOM 的脚本。

function pasteSelection() {
    //Select current tab to send message
    chrome.tabs.query({
        "active": true,
        "currentWindow": true,
        "status": "complete",
        "windowType": "normal"
    }, function (tabs) {
        //It returns array so looping over tabs result
        for (tab in tabs) {

            //Send Message to a tab
            chrome.tabs.sendMessage(tabs[tab].id, {
                method: "getSelection"
            });
        }
    });
}
//Adding a handler when message is recieved from content scripts
chrome.extension.onMessage.addListener(function (response, sender) {

    //Set text to text area
    var text = document.getElementById('text');
    text.value = response.data;
});

// Bind On click event to pasteSelection() function
document.addEventListener("DOMContentLoaded", function () {

    document.getElementById("submit").onclick = pasteSelection;
});

选择.js

将选定的文本传递给popup.html

 //Add a handler to handle message sent from popup.html
 chrome.extension.onMessage.addListener(function (request, sender) {
     //Hanlde request based on method
     if (request.method == "getSelection")
     //Send selected text back to popup.html
     chrome.extension.sendMessage({
         data: document.getSelection().toString()
     });
     else chrome.extension.sendMessage({}); // snub them.
 });

参考

于 2013-01-08T05:33:36.647 回答