是否可以在Chrome扩展程序中添加.XML文件来存储/检索数据?

时间:2019-01-20 11:05:06

标签: javascript json xml google-chrome-extension manifest

我已经为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时,应动态更改 [括号中的内容]

  1. 我的.js文件如何访问XML文件? (任何示例code都会受到赞赏)

  2. 如何将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扩展根文件夹中

1 个答案:

答案 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>

希望它对某人有帮助