Snippet to Endpoint: JSON-LD Support and Multiple Endpoints

While cleaning up the code of my original Snippet to Endpoint extension, I revisited a feature, namely the ability to handle structured data, I had wanted for some time. Many modern sites already describe their content using JSON-LD, making it far more practical to extract and send that data directly to an endpoint instead of parsing HTML afterward.

This update adds JSON-LD extraction, multiple endpoint configurations, and selection options in the preview window.


JSON-LD Extraction

The extension now detects all <script type="application/ld+json"> blocks, parses their content, and flattens any arrays or @graph structures into a single list of entities. Each entity is stored as a stringified JSON object for easy sending.

// Extract and flatten all JSON-LD snippets
const jsonLdSnippets = [];
const addEntity = (entity) => {
    try {
        jsonLdSnippets.push(JSON.stringify(entity));
    } catch (e) {
        console.warn('Error stringifying JSON-LD entity:', e);
    }
};

document.querySelectorAll('script[type="application/ld+json"]').forEach((el, i) => {
    try {
        let data = JSON.parse(el.textContent);
        if (Array.isArray(data)) {
            data.forEach(addEntity);
        } else if (data['@graph']) {
            data['@graph'].forEach(addEntity);
        } else {
            addEntity(data);
        }
    } catch (err) {
        console.warn(`Error parsing JSON-LD script #${i}`, err);
    }
});

Preview and Selection

The preview window now allows selecting:

  • whether to send the sanitized HTML with the extracted JSON-LD entity, and
  • which endpoint the data should be sent to.

Multiple Endpoints

The configuration page supports defining multiple endpoints with independent settings (URL, authentication token). When sending data, an endpoint can be chosen from within the preview window.


Video

Repository

https://github.com/MaMuk/FirefoxSnippetToEndpoint

I also got it approved to be published on addons.mozilla.org