我已经为Office构建了Chrome扩展程序,有时需要存储/检索基本数据,并且想到了将一个简单的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>
标签。
当XML
读取javascript
时,应动态更改 [括号中的内容] 。
我的.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" ,
返回错误,指示$.ajax
在远程服务器中寻找xmlFile
,但不在 Chrome扩展根文件夹中
答案 0 :(得分:0)
花了一段时间,但终于奏效了。
这就是我所做的。
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>
希望它对某人有帮助