I am developing a Chrome extension that needs to package an XML file (a trie data structure) and be able to read that file from the content script. So the content script will instantiate the trie after reading the data from the XML file every time the extension is loaded.
How to read this XML file through content script (or background page)? Do I need to use localStorage?
User data can be automatically synced with Chrome sync (using storage. sync ). Your extension's content scripts can directly access user data without the need for a background page.
It's as simple as Right Click > getURL. Open up the Extension popup window and you will be greeted with the parameters in a nicely formatted table. You can now even export the parameters to both CSV and TSV format, simply visit the Export Tab. --- Version: 1.0.
To inject a content script programmatically, your extension needs host permissions for the page it's trying to inject scripts into. Host permissions can either be granted by requesting them as part of your extension's manifest (see host_permissions ) or temporarily via activeTab.
Often, it is desirable for a Chrome extension to be bundled with files that need to be read. These files may contain data or configuration information to help the extension function. This short guide will show you how you can set up your Chrome extension to read files.
A simple ajax request to the XML file should give you the XML DOM nodes:
function request(url) {
var xhr = new XMLHttpRequest();
try {
xhr.onreadystatechange = function(){
if (xhr.readyState != 4)
return;
if (xhr.responseXML) {
console.debug(xhr.responseXML);
}
}
xhr.onerror = function(error) {
console.debug(error);
}
xhr.open("GET", url, true);
xhr.send(null);
} catch(e) {
console.error(e);
}
}
function init() {
request("sample.xml");
}
You would need to use a js-based xml parser or write your own. It might be easier to save your data as a JSON object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With