Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emscripten: ofLaunchBrowser implementation #7621

Closed
wants to merge 11 commits into from
68 changes: 42 additions & 26 deletions libs/openFrameworks/utils/ofUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
#define MAXPATHLEN 1024
#endif

#ifdef TARGET_EMSCRIPTEN
#include <emscripten.h>
#endif

using std::vector;
using std::string;
using std::setfill;
Expand Down Expand Up @@ -903,15 +907,14 @@ size_t ofUTF8Length(const std::string & str){
}
}

//--------------------------------------------------
void ofLaunchBrowser(const string& url, bool uriEncodeQuery){
std::string ofSanitizeURLString(const std::string& url, bool uriEncodeQuery) {
UriParserStateA state;
UriUriA uri;
state.uri = &uri;
if(uriParseUriA(&state, url.c_str())!=URI_SUCCESS){
ofLogError("ofUtils") << "ofLaunchBrowser(): malformed url \"" << url << "\"";
uriFreeUriMembersA(&uri);
return;
return "";
}
if(uriEncodeQuery) {
uriNormalizeSyntaxA(&uri); // URI encodes during set
Expand All @@ -925,50 +928,63 @@ void ofLaunchBrowser(const string& url, bool uriEncodeQuery){
std::string uriStr(buffer.data(), written-1);
uriFreeUriMembersA(&uri);


// http://support.microsoft.com/kb/224816
// make sure it is a properly formatted url:
// some platforms, like Android, require urls to start with lower-case http/https
// Poco::URI automatically converts the scheme to lower case
if(scheme != "http" && scheme != "https"){
ofLogError("ofUtils") << "ofLaunchBrowser(): url does not begin with http:// or https://: \"" << uriStr << "\"";
return;
return "";
}
return uriStr;
}

#ifdef TARGET_WIN32
#ifndef TARGET_EMSCRIPTEN
//--------------------------------------------------
void ofLaunchBrowser(const string& url, bool uriEncodeQuery){
auto uriStr = ofSanitizeURLString(url, uriEncodeQuery);
if (uriStr != "") {
#ifdef TARGET_WIN32
ShellExecuteA(nullptr, "open", uriStr.c_str(),
nullptr, nullptr, SW_SHOWNORMAL);
#endif

#ifdef TARGET_OSX
// could also do with LSOpenCFURLRef
nullptr, nullptr, SW_SHOWNORMAL);
#endif
#ifdef TARGET_OSX
// could also do with LSOpenCFURLRef
string commandStr = "open \"" + uriStr + "\"";
int ret = system(commandStr.c_str());
if(ret!=0) {
if(ret!=0) {
ofLogError("ofUtils") << "ofLaunchBrowser(): couldn't open browser, commandStr \"" << commandStr << "\"";
}
#endif

#ifdef TARGET_LINUX
#endif
#ifdef TARGET_LINUX
string commandStr = "xdg-open \"" + uriStr + "\"";
int ret = system(commandStr.c_str());
if(ret!=0) {
ofLogError("ofUtils") << "ofLaunchBrowser(): couldn't open browser, commandStr \"" << commandStr << "\"";
}
#endif

#ifdef TARGET_OF_IOS
#endif
#ifdef TARGET_OF_IOS
ofxiOSLaunchBrowser(uriStr);
#endif

#ifdef TARGET_ANDROID
#endif
#ifdef TARGET_ANDROID
ofxAndroidLaunchBrowser(uriStr);
#endif

#ifdef TARGET_EMSCRIPTEN
ofLogError("ofUtils") << "ofLaunchBrowser() not implementeed in emscripten";
#endif
#endif
}
}
#else
void ofLaunchBrowser(const string& url, bool uriEncodeQuery, std::string target){
auto uriStr = ofSanitizeURLString(url, uriEncodeQuery);
if (uriStr != "") {
EM_ASM_({
window.open(UTF8ToString($0), UTF8ToString($1));
}, uriStr.c_str(), target.c_str());
}
}
#endif

//--------------------------------------------------
string ofGetVersionInfo(){
Expand Down
24 changes: 22 additions & 2 deletions libs/openFrameworks/utils/ofUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1056,13 +1056,33 @@ void ofSaveViewport(const std::string& filename);

/// \section System

/// \brief Launch the given URL in the default browser.
/// \brief Process the string into an actionable URL
///
/// \param url the URL to open.
/// \param url the URL to process.
/// \param uriEncodeQuery true if the query parameters in the given URL have
/// already been URL encoded.
/// \returns a string if the preparation is successful, emtpy if not
std::string ofSanitizeURLString(const std::string& url, bool uriEncodeQuery=false);

#ifndef TARGET_EMSCRIPTEN

/// \brief Launch the given URL in the default browser, or within itself in the case of emscripten.
///
/// \param url the URL to open.
/// \param uriEncodeQuery true if the query parameters in the given URL have
/// already been URL encoded.
void ofLaunchBrowser(const std::string& url, bool uriEncodeQuery=false);

#else

/// \brief Opens an URL in a new browser tab (or other behaviour depending on target value)
///
/// \param url the URL to open.
/// \param uriEncodeQuery true if the query parameters in the given URL have
/// already been URL encoded.
/// \param target defaults to '_blank', behaves like the target of the javascript open function (e.g. use "_self" to replace content).
void ofLaunchBrowser(const std::string& url, bool uriEncodeQuery=false, std::string target = "_blank");

#endif

/// \brief Executes a system command. Similar to run a command in terminal.
Expand Down