0

我正在开发一个 chrome 扩展,我需要从任何页面获取选定的文本到 chrome 扩展。我无法获得输出

我只想将选定的图像或任何文本用于扩展

在控制台中出现以下错误

未捕获的 IndexSizeError:索引或大小为负数,或大于允许的值。

//script.js 

var selection = window.getSelection();
var range = selection.getRangeAt(0); //Getting error in this line.window.getSelection() is always null
if (range) {
    var div = document.createElement('div');
    div.appendChild(range.cloneContents());
    vs=div.innerHTML;
} 


chrome.extension.sendRequest({viewsource: vs}, function(response) {
    console.log(response.farewell);
});


chrome.tabs.executeScript(null, {file:"script.js"});

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        document.getElementById("txtar").innerText=request.viewsource;
});


//popup.html

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="script.js"></script>


        <style>
            body { 
                margin:0px; 
                padding:0px; 
                overflow:hidden;
                 background:pink;
                }

            #txtar { 
                height: 200px; 
                width: 300px; 
                max-width:780px; 
                max-height:560px; 
                min-width:300px; 
                min-height:200px; 
                padding:6px; 
                margin:0px; 
                background-color:white; 
                color:#000088; 
                border:none;
                }

            #boohle {
                position:absolute; 
                bottom:0px; 
                left:0px; 
                font-size:10px; 
                font-family:Arial; 
                float:left; 
                background-color:white; 
                z-index:200; 
                font-weight:normal;
                }

            #boohle a {
                color:#008800; 
                text-decoration:none;
                }

            #boohle a:hover {
                color:#ff0000; 
                }
        </style>
    </head>
    <body>
        <textarea id="txtar" readonly></textarea>
    </body>
</html>

//manifest.json

{
    "name": "Selection",
    "version": "1.0",

    "browser_action": {
        "default_icon": "img/19x19.png",
        "default_title": "View Selection Source",
        "default_popup": "popup.html"
    },

    "manifest_version": 2,
    "description": "View selection source in resizable popup. Drag the bottom right corner to resize. Simple, but very useful for web developers.",
    "icons": {
        "128": "img/128x128.png",
        "16": "img/16x16.png",
        "19": "img/19x19.png",
        "32": "img/32x32.png",
        "48": "img/48x48.png"
    }

}

请帮忙。

4

0 回答 0