0

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?

Deqing
  • 14,098
  • 15
  • 84
  • 131

2 Answers2

0

There are two ways you could address this; the first is to simply use javascript to get the path and then pass it into the plugin in a method call.

The second is to get the path to the plugin .dll file and then calculate the location of config.txt relative to that as explained on the website.

I suppose the other option would be to try to use:

m_host->getDOMWindow()->getNode("chrome")->getNode("extension")->getNode("getURL")->callMethod<FB::variant>("", FB::variant_list_of("config.txt");

I don't know if that would actually work or not; I haven't tried it, and I may have even mistyped some of the function calls, but it might be worth a try.

taxilian
  • 14,229
  • 4
  • 34
  • 73
  • Thanks Taxilian. I'm afraid in Chrome extension we can't get real path of a file, which required by those open file functions. – Deqing Dec 05 '11 at 07:12
  • Okay; well, you could still have gotten the path of the plugin and found it relative. Either way, I'm glad you figured something out. – taxilian Dec 05 '11 at 15:48
0

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__");
}
Deqing
  • 14,098
  • 15
  • 84
  • 131