Search code examples
javascriptgoogle-chrome-extensionfirefox-addon-webextensionsfetch-api

How to use fetch to get the HTML source of a webpage in a firefox or chrome web extension?


I am trying to use fetch to get the HTML source of https://smmry.com/ in a Chrome/Firefox web extension.

Here is my manifest.json

{

    "manifest_version": 2,
    "name": "To_Be_Done",
    "version": "1.0",

    "description": "To_Be_Done",

    "permissions": [
        "tabs",
        "*://*.smmry.com/*"
    ],

    "icons": {
        "48": "icons/border-48.png"
    },

    "browser_action": {
        "browser_style": true,
        "default_popup": "popup/choose_page.html",
        "default_icon": {
            "16": "icons/news-icon-16.png",
            "32": "icons/news-icon-32.png"
        }
    }
}

Whenever, my extension's button is clicked, I want to get the HTML source of smmry.com. However, I don't know how to implement the fetch() method to do this. I've read through the documentation but I am still confused.

Can anyone show an example?


Solution

  • I figured out how to do it. The following returns the html file of "https://smmry.com" as a string.

    fetch('https://smmry.com/')
      .then((resp) => resp.text())
      .then((data) => {         
      })