Ask Your Question
2

How can I obtain all the webpage resources using a Chrome extension?

asked 2021-07-08 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-06-26 05:00:00 +0000

lakamha gravatar image

To obtain all the webpage resources using a Chrome extension, you can use the Chrome developer tools API. Here's how:

  1. Create a new Chrome extension using the manifest.json and background.js files.
  2. In the background.js file, use the chrome.devtools.network.onRequestFinished event to listen for network requests made by the webpage.
  3. When a request is finished, get the request details using the chrome.devtools.network.getHAR() function.
  4. Extract the resource URLs from the request details and store them in an array or object.
  5. Use the chrome.runtime.sendMessage() function to send the resource URLs to the popup or content script of the extension, where you can display or use them however you want.

Here's a sample code snippet that shows how to get the resource URLs:

chrome.devtools.network.onRequestFinished.addListener(
  function(request) {
    var har = chrome.devtools.network.getHAR(function(result) {
      var entries = result.entries;
      for (var i = 0; i < entries.length; i++) {
        var entry = entries[i];
        var url = entry.request.url;
        // Store the URL in an array or object
      }
      // Send the array or object to the popup or content script
      chrome.runtime.sendMessage({resources: resources});
    });
  }
);
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-08 11:00:00 +0000

Seen: 19 times

Last updated: Jun 26 '22