Estoy tratando de obtener un archivo .json simple de mis recursos accesibles en mi secuencia de comandos de contenido. Esto es lo que probé hasta ahora:
manifiesto.json
{ "manifest_version": 3, "name": "MyExtension", "version": "0.0", "content_scripts": [{ "matches": ["*://*/*"], "js": ["content.js"] }], "icons":{ "128": "images/logo128x128.png" }, "web_accessible_resources": [{ "resources": ["data/data.json"], "matches": ["https://web-accessible-resources-1.glitch.me/*"] }] }contenido.js
const jsondict_url = chrome.runtime.getURL("data.json") console.log(jsondict_url) var jsondict = fetch(jsondict_url) console.log(jsondict)Este es el mensaje de error que recibo:
chrome-extension://hbjmkaohgphjcegliejkcehbmjeggnnb/data.json physique-generale-mecanique-PHYS-101-A#:1 Denying load of chrome-extension://hbjmkaohgphjcegliejkcehbmjeggnnb/data.json. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension. content.js:6 Promise {<pending>}[[Prototype]]: Promise [[PromiseState]]: "rejected" [[PromiseResult]]: TypeError: Failed to fetch at chrome-extension://hbjmkaohgphjcegliejkcehbmjeggnnb/content.js:4:16 content.js:4 GET chrome-extension://invalid/ net::ERR_FAILED (anonymous) @ content.js:4 content.js:4 Uncaught (in promise) TypeError: Failed to fetch at content.js:4:16 (anonymous) @ content.js:4Editar: mis archivos están organizados de esta manera
imágenes/ logo128x128.png
datos/datos.json
contenido.js
manifiesto.js
Corregir la ruta en 'content.js' a 'data/data.js' ayudó.
Encontré una solución a mi problema, creo que estaba haciendo un mal uso de la función de búsqueda. Esta introducción https://developers.google.com/web/updates/2015/03/introduction-to-fetch utiliza el siguiente código:
fetch('./api/some.json') // 'data/data.json' in my case .then( function(response) { if (response.status !== 200) { console.log('Looks like there was a problem. Status Code: ' + response.status); return; } // Examine the text in the response response.json().then(function(data) { console.log(data); }); } ) .catch(function(err) { console.log('Fetch Error :-S', err); });Mi extensión funciona bien con esto.
Está intentando getURL("data.json") pero su manifiesto indica "resources": ["data/data.json"] entonces la ruta es incorrecta en al menos un lugar? Debería ser getURL("data/data.json") .
También puede intentar agregar content.js a "web_accessible_resources" .