Chrome Extension - Get entire text content of the current tab

后端 未结 2 1680
别那么骄傲
别那么骄傲 2021-02-01 04:27

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 essenc

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-01 04:48

    Use executeScript: (requires permission activeTab)

    chrome.tabs.executeScript(null, {
        code: `document.all[0].innerText`,
        allFrames: false, // this is the default
        runAt: 'document_start', // default is document_idle. See https://stackoverflow.com/q/42509273 for more details.
    }, function(results) {
        // results.length must be 1
        var result = results[0];
        process_result(result);
    });
    

    In case the code is complex, it's possible to define a function in the content script and call that function in the code (or use file).

提交回复
热议问题