简体   繁体   中英

Chrome extension: DOMParser is not defined with Manifest v3

I have developped an extension to scrape some content from web page and up to now it was working fine but since I switched to manifest v3, the parsing doesn't work anymore.

I use the following script to read the source code:

chrome.scripting.executeScript( 
  {
    target: {tabId: tab.id, allFrames: true},
    files: ['GetSource.js'],
  }, async function(results) 
  {
    // GETTING HTML
    parser = new DOMParser();
    content = parser.parseFromString(results, "text/html");

... ETC... This code used to work fine but now I get the following message in my console:

Uncaught (in promise) ReferenceError: DOMParser is not defined

The code is part of a promise but I don't think the promise is the problem here. I basically need to load the source code into a variable so that I can parse it afterwards.

I've checked the documentation but I haven't found something mentionned that DOMParser was not going to work with v3.

Any idea?

Thanks

Since service workers don't have access to DOM, it's not possible for an extension's service worker to access the DOMParser API or create an

to parse and traverse documents.

More detail

And I solve the problem by using library dom-parser.The code could be like this

import DomParser from "dom-parser";
const parser = new DomParser();
const dom = parser.parseFromString('you html string');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM