Extract the whole text contained in webpage using Chrome extension

后端 未结 1 1193
借酒劲吻你
借酒劲吻你 2020-12-06 08:38

I\'m developing a Chrome extension for text parsing of Google search results. I want the user to insert a certain text in the omnibox, and then be direct to a Google search

相关标签:
1条回答
  • 2020-12-06 09:28

    Well, parsing the webpage is probably going to be easier to do as a DOM instead of plain text. However, that is not what your question asked.

    Your code has issues with how you are navigating to the page and dealing with the asynchronous nature of web navigation. This is also not what your question asked, but impacts how what you did ask about, getting text from a webpage, is implemented.

    As such, to answer your question of how to extract the plain text from a webpage, I implemented doing so upon the user clicking a browser_action button. This separates answering how this can be done from the other issues in your code.

    As wOxxOm mentioned in a comment, to have access to the DOM of a webpage, you have to use a content script. As he did, I suggest you read the Overview of Chrome extensions. You can inject a content script using chrome.tabs.executeScript. Normally, you would inject a script contained in a separate file using the file property of the details parameter. For code that is just the simple task of sending back the text of the webpage (without parsing, etc), it is reasonable to just insert the single line of code that is required for the most basic way of doing so. To insert a short segment of code, you can do so using the code property of the details parameter. In this case, given that you have said nothing about your requirements for the text, document.body.innerText is the text returned.

    To send the text back to the background script, chrome.runtime.sendMessage() is used.

    To receive the text in the background script, a listener, receiveText, is added to chrome.runtime.onMessage.

    background.js:

    chrome.browserAction.onClicked.addListener(function(tab) {
        console.log('Injecting content script(s)');
        //On Firefox document.body.textContent is probably more appropriate
        chrome.tabs.executeScript(tab.id,{
            code: 'document.body.innerText;'
            //If you had something somewhat more complex you can use an IIFE:
            //code: '(function (){return document.body.innerText})();'
            //If your code was complex, you should store it in a
            // separate .js file, which you inject with the file: property.
        },receiveText);
    });
    
    //tabs.executeScript() returns the results of the executed script
    //  in an array of results, one entry per frame in which the script
    //  was injected.
    function receiveText(resultsArray){
        console.log(resultsArray[0]);
    }
    

    manifest.json:

    {
        "description": "Gets the text of a webpage and logs it to the console",
        "manifest_version": 2,
        "name": "Get Webpage Text",
        "version": "0.1",
    
        "permissions": [
            "activeTab"
        ],
    
        "background": {
            "scripts": [
                "background.js"
            ]
        },
    
        "browser_action": {
            "default_icon": {
                "32": "myIcon.png"
            },
            "default_title": "Get Webpage Text",
            "browser_style": true
        }
    }
    
    0 讨论(0)
提交回复
热议问题