Search code examples
google-chrome-extensionnpapifirebreath

How to open a file which includes in Chrome extension by C/C++?


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?


Solution

  • 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__");
    }