如何通过C / C ++打开包含在Chrome扩展中的文件? [英] How to open a file which includes in Chrome extension by C/C++?
问题描述
我试图在我的Chrome扩展程序中打开一个文件来解析,比如说config.txt。
通过Javascript,它会相当容易。例如: chrome.extension.getURL('config.txt')
会得到类似于 chrome-extension://kfcphocilcidmjolfgicbchdfjjkmkmh/config.txt
。
但是,在扩展的C ++(或C)代码中,通过这种URL打开文件不可用。 p>
在这种情况下,是否有任何方法可以打开扩展文件?
我最终设法找到了一种方法,通过XmlHttpRequest获取我的NPAPI插件中的config.txt。
const char * getconfig =
window .__ config_txt__ =(function(){
var ret = \Default return value\;
var xhr = new XMLHttpRequest();
xhr.open(\GET\,\config.txt\,false);
xhr.onreadystatechange = function(){
;
{
ret = xhr.responseText;
};
xhr.send() ;
返回ret;
} )();;
m_host-> evaluateJavaScript(getconfig);
if(window& window> getJSObject() - > HasProperty(window)){
std :: string content = window-> getProperty< std ::串GT;( __ config_txt__);
}
I'm trying to open a file to parse, say "config.txt", in my Chrome extension.
By Javascript it will be fairly easy. For example: chrome.extension.getURL('config.txt')
will get something like chrome-extension://kfcphocilcidmjolfgicbchdfjjlfkmh/config.txt
.
However, in the C++(or C) code of the extension, open a file by this kind of URL is not available.
Is there any way to open a file in extension in this case?
I finally managed to find a way to get the "config.txt" in my NPAPI plugin by XmlHttpRequest.
const char* getconfig =
" window.__config_txt__ = (function() {"
" var ret = \"Default return value\";"
" var xhr = new XMLHttpRequest();"
" xhr.open(\"GET\", \"config.txt\", false);"
" xhr.onreadystatechange = function() {"
" if(xhr.readyState == 4) {"
" ret = xhr.responseText;"
" };"
" };"
" xhr.send();"
" return ret;"
" })();";
m_host->evaluateJavaScript(getconfig);
if (window && window->getJSObject()->HasProperty("window")) {
std::string content = window->getProperty<std::string>("__config_txt__");
}
这篇关于如何通过C / C ++打开包含在Chrome扩展中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!