Exprt XMP website scraped data to file

I am a swift coder, not a JavaScript coder. But, with the help of ChatGPT I am slowly getting there.

I am trying to create a Firefox extension that will export an XMP file scaped from specific data on a webpage.

const parser = new DOMParser();
const xmpDocument = parser.parseFromString(xmpData, 'text/xml');

const subjectReference = xmpDocument.querySelector('Iptc4xmpCore\\:SubjectReference').textContent;
const headline = xmpDocument.querySelector('photoshop\\:Headline').textContent;
const description = xmpDocument.querySelector('dc\\:description rdf\\:li').textContent;
const subject = xmpDocument.querySelector('dc\\:subject rdf\\:li').textContent;

console.log('Subject Reference:', subjectReference);
console.log('Headline:', headline);
console.log('Description:', description);
console.log('Subject:', subject);

I have the base manifest.json, popup HTML and JS files for a basic FF extension, but not familliar with the process.

Can anybody please advise ?

I’d love to help you but I’m not as familiar with xmp data at all. I’m not even sure what data you are trying to even scrape.

Thank you for your interest.

Originally I wanted the title of the webpage, the URL and any data currently selected by the cursor.

Those three fields would then be exported into the XMP (Adboe) format into a file.

The code I have as follows should do it, but I’m not familliar with JavaScript and how to put it all together:

// Create a new XMP document
const xmpDocument = document.implementation.createDocument(null, 'x:xmpmeta', null);

// Create the root element
const xmpRoot = xmpDocument.documentElement;
xmpRoot.setAttribute('xmlns:x', 'adobe:ns:meta/');
xmpRoot.setAttribute('x:xmptk', 'XMP toolkit 2.9-9, framework 1.6');

// Create RDF element
const rdf = xmpDocument.createElementNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdf:RDF');

// Create Iptc4xmpCore element
const iptcElement = xmpDocument.createElement('rdf:Description');
iptcElement.setAttribute('rdf:about', '');
iptcElement.setAttribute('xmlns:Iptc4xmpCore', 'http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/');

// Create SubjectReference element
const subjectReference = xmpDocument.createElement('Iptc4xmpCore:SubjectReference');
subjectReference.textContent = 'http://speiselift.com/2011/05/die-tasse-im-fuchspelz/';

// Create SubjectCode element
const subjectCode = xmpDocument.createElement('Iptc4xmpCore:SubjectCode');

// Create Bag element
const bag = xmpDocument.createElement('rdf:Bag');

// Create Bag's rdf:li element
const li = xmpDocument.createElement('rdf:li');
li.textContent = 'http://speiselift.com/2011/05/die-tasse-im-fuchspelz/';

// Create Photoshop element
const photoshopElement = xmpDocument.createElement('rdf:Description');
photoshopElement.setAttribute('rdf:about', '');
photoshopElement.setAttribute('xmlns:photoshop', 'http://ns.adobe.com/photoshop/1.0/');

// Create Headline element
const headline = xmpDocument.createElement('photoshop:Headline');
headline.textContent = 'Die Tasse im Fuchspelz Speiselift';

// Create DC element
const dcElement = xmpDocument.createElement('rdf:Description');
dcElement.setAttribute('rdf:about', '');
dcElement.setAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');

// Create Description element
const description = xmpDocument.createElement('dc:description');
const alt = xmpDocument.createElement('rdf:Alt');
const li = xmpDocument.createElement('rdf:li');
li.setAttribute('xml:lang', 'x-default');
li.textContent =
  'Gegen diesen Pelz haben Tierschützer sicher nichts einzuwenden: Das Pariser Label Kitsuné hat als Hommage an den eigenen Namen (bedeutet Rotfuchs auf Japanisch) diese Tasse mit dem Namen Fur Mug kreiert.';

// Create Subject element
const subject = xmpDocument.createElement('dc:subject');
const subjectBag = xmpDocument.createElement('rdf:Bag');
const subjectLi = xmpDocument.createElement('rdf:li');
subjectLi.textContent = 'pax';

// Serialize the XMP document to a string
const xmpSerializer = new XMLSerializer();
const xmpString = xmpSerializer.serializeToString(xmpDocument);

// Output the XMP data
console.log('Generated XMP data:');