且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Chrome扩展程序 - 获取当前选项卡的全部文本内容

更新时间:2023-12-05 19:17:34

您可以使用 document.body.innerText document.all [0] .innerText 内容脚本

它将获得所有 / em> 在页面中,没有任何HTML代码。



或者您可以使用 document.all [0] .outerHTML 来获取整个页面的HTML。






示例



在内容脚本中

 函数getText(){
返回document.body.innerText
}
函数getHTML(){
return document.body.outerHTML
}
console.log(getText()); //给你所有页面上的文字
console.log(getHTML()); //给出页面的整个HTML






已添加



所以您希望内容脚本将 文本 返回到弹出窗口。您可以使用:


  • chrome.tabs.getSelected 来获取选中的选项卡,

  • chrome.tabs.sendRequest 向内容脚本发送请求,

  • chrome.extension.onRequest.addListener 来侦听请求。



弹出页面

  chrome.tabs.getSelected(null,function(tab){
chrome.tabs .sendRequest(tab.id,{method:getText},function(response){
if(response.method ==getText){
alltext = response.data;
}
});
});

内容脚本

  chrome.extension.onRequest.addListener(
函数(request,sender,sendResponse){
if(request.method ==getText){
sendResponse({data:document.all [0] .innerText,method:getText}); //与innerText相同
}
}
);

这应该有效。


I'm developing an extension where i need to get the entire text content on the current tab. Now i've a plugin which retrieves selected text from the current tab. So, in essence i'm looking for the ctrl-A version of it :). This is what i've done so far taking the hint from @Derek.

This is in my event handler(this is just one, there are other listeners too for onUpdated etc):

chrome.tabs.onSelectionChanged.addListener(function(tabId,changeInfo,tab){  
chrome.tabs.getSelected(null,function(tab){
chrome.tabs.sendRequest(tab.id, {method: "getSelection"}, function (response) {
  selectedtext = response.data;
});
chrome.tabs.sendRequest(tab.id, {method: "getText"}, function (response) {
  alltext = response.data;
});
});
});

This is what i've written in the content script:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "getSelection")
  sendResponse({data: window.getSelection().toString()});
else if (request.method == "getText")
  sendResponse({data: document.body.innerText});
else 
  sendResponse({});
});

However the document.body.innerText is returning undefined. I need the entire text of the current tab in alltext. Can someone help me out on this? Thanks.

You can use document.body.innerText or document.all[0].innerText to do it in the content script.
It will get all the text content in the page, without any HTML code.

Or you can use document.all[0].outerHTML to get the HTML of the whole page.


Example

In the Content Script

function getText(){
    return document.body.innerText
}
function getHTML(){
    return document.body.outerHTML
}
console.log(getText());             //Gives you all the text on the page
console.log(getHTML());             //Gives you the whole HTML of the page


Added

So you want the content script to return the text to the popup. You can use:

  • chrome.tabs.getSelected to get the tab selected,
  • chrome.tabs.sendRequest to send request to the content script,
  • and chrome.extension.onRequest.addListener to listen to requests.

Popup page

chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {method: "getText"}, function(response) {
        if(response.method=="getText"){
            alltext = response.data;
        }
    });
});

Content Script

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        if(request.method == "getText"){
            sendResponse({data: document.all[0].innerText, method: "getText"}); //same as innerText
        }
    }
);

This should work.