是否可以在 Chrome 扩展中向 store/retrieve 数据添加 .XML 文件?
Is it possible to add an .XML file in Chrome Extension to store/retrieve data?
我已经为我的 Office 构建了一个 Chrome 扩展,在某些时候,我需要 store/retrieve 基本数据,我想在同一个文件夹中有一个简单的 XML
文件使用 manifest
和 js
文件。
类似于:
<templates>
<template>
<title> Invoice sent</title>
<text>
Dear [CUSTOMER_NAME],
We have just issued you an invoice.
Kindly note that the total amount does not include the Pre-export
inspection fee.
**Please get back to us soonest possible, so we can reserve the
[PRODUCT_NAME] for you and avoid having it
sold to a different client.
Regards
[STAFF_NAME]
[DEPARTMENT_NAME]
</text>
</template>
<template>
<title>........
</templates>
<templates>
标签将有多个<template>
标签,其中包含不同的<title>
和 <text>
标签 。
[括号中的内容] 应在 javascript
读取 XML
时动态更改。
我的 .js
文件如何访问 XML
文件? (任何样本 code
将不胜感激)
如何将XML
文件添加到manifest.json
文件中,以便content002.js
文件可以访问?
到目前为止 manifest.json
看起来像:
{
"manifest_version": 2,
"name": "myExtName",
"version": "0.1.0",
"description": "My Browser extension",
"content_scripts": [{
//"css": ["styles.css"],
"js": ["content002.js","jquery.js"],
"matches": ["https://www.companysite.com/*","file://*"],
"run_at": "document_end"
}]
}
我试过了: How to read xml file contents in jQuery and display in html elements?
但到目前为止还没有成功,显然是在使用:
$.ajax({
type: "GET" ,
url: "xmlFile.xml" ,
dataType: "xml" ,
returns 一个错误,表明 $.ajax
正在 远程服务器 中寻找 xmlFile
,但不在 Chrome 扩展名 根文件夹
花了一些时间,但终于成功了。
这是我所做的。
manifest.json
{
"manifest_version": 2,
"name": "EXTENSION NAME",
"version": "0.1.0",
"description": "DESCRIPTION GOES HERE",
"content_scripts": [{
"js": ["jq3.js","JAVASCRIPT.js"],
"matches": ["https://*/*","file://*"],
"run_at": "document_end"
}],
"web_accessible_resources":
["xmlFile.xml"] //This .xml file is in the same directory with the manifest
}
JAVASCRIPT
var xmlCont = chrome.runtime.getURL("xmlFile.xml"); //this returns the FULL_PATH of the .xml file [NOT THE XML NODES]
console.log(xmlCont) //You can see it here
$.ajax({
type: "GET" ,
url: xmlCont,//Place that FULL_PATH here
dataType: "xml" , //This too is important
success: function(xml) {
console.log(xml)// see the ajax results if your curious
//var xmlDoc = $.parseXML( xml );//NOT NEED FOR THIS
//console.log(xmlDoc);// if you parseXML it returns null
var title = $(xml).find('value').text(); //Show the text between the <title> tags
console.log(title);
}
});
XML
<rss version='2.0'>
<channel>
<title>
I am the text between the -title- tag
</title>
</channel>
</rss>
希望对大家有帮助
我已经为我的 Office 构建了一个 Chrome 扩展,在某些时候,我需要 store/retrieve 基本数据,我想在同一个文件夹中有一个简单的 XML
文件使用 manifest
和 js
文件。
类似于:
<templates>
<template>
<title> Invoice sent</title>
<text>
Dear [CUSTOMER_NAME],
We have just issued you an invoice.
Kindly note that the total amount does not include the Pre-export
inspection fee.
**Please get back to us soonest possible, so we can reserve the
[PRODUCT_NAME] for you and avoid having it
sold to a different client.
Regards
[STAFF_NAME]
[DEPARTMENT_NAME]
</text>
</template>
<template>
<title>........
</templates>
<templates>
标签将有多个<template>
标签,其中包含不同的<title>
和 <text>
标签 。
[括号中的内容] 应在 javascript
读取 XML
时动态更改。
我的
.js
文件如何访问XML
文件? (任何样本code
将不胜感激)如何将
XML
文件添加到manifest.json
文件中,以便content002.js
文件可以访问?
到目前为止 manifest.json
看起来像:
{
"manifest_version": 2,
"name": "myExtName",
"version": "0.1.0",
"description": "My Browser extension",
"content_scripts": [{
//"css": ["styles.css"],
"js": ["content002.js","jquery.js"],
"matches": ["https://www.companysite.com/*","file://*"],
"run_at": "document_end"
}]
}
我试过了: How to read xml file contents in jQuery and display in html elements?
但到目前为止还没有成功,显然是在使用:
$.ajax({
type: "GET" ,
url: "xmlFile.xml" ,
dataType: "xml" ,
returns 一个错误,表明 $.ajax
正在 远程服务器 中寻找 xmlFile
,但不在 Chrome 扩展名 根文件夹
花了一些时间,但终于成功了。
这是我所做的。
manifest.json
{
"manifest_version": 2,
"name": "EXTENSION NAME",
"version": "0.1.0",
"description": "DESCRIPTION GOES HERE",
"content_scripts": [{
"js": ["jq3.js","JAVASCRIPT.js"],
"matches": ["https://*/*","file://*"],
"run_at": "document_end"
}],
"web_accessible_resources":
["xmlFile.xml"] //This .xml file is in the same directory with the manifest
}
JAVASCRIPT
var xmlCont = chrome.runtime.getURL("xmlFile.xml"); //this returns the FULL_PATH of the .xml file [NOT THE XML NODES]
console.log(xmlCont) //You can see it here
$.ajax({
type: "GET" ,
url: xmlCont,//Place that FULL_PATH here
dataType: "xml" , //This too is important
success: function(xml) {
console.log(xml)// see the ajax results if your curious
//var xmlDoc = $.parseXML( xml );//NOT NEED FOR THIS
//console.log(xmlDoc);// if you parseXML it returns null
var title = $(xml).find('value').text(); //Show the text between the <title> tags
console.log(title);
}
});
XML
<rss version='2.0'>
<channel>
<title>
I am the text between the -title- tag
</title>
</channel>
</rss>
希望对大家有帮助