mirror of https://github.com/vitalif/phantomjs
Add option --script-language to explicitly set javascript or coffeescript
https://github.com/ariya/phantomjs/issues/117441.x
parent
d5eaf41063
commit
394e2f8699
|
@ -65,6 +65,7 @@ static const struct QCommandLineConfigEntry flags[] =
|
|||
{ QCommandLine::Option, '\0', "proxy-auth", "Provides authentication information for the proxy, e.g. ''-proxy-auth=username:password'", QCommandLine::Optional },
|
||||
{ QCommandLine::Option, '\0', "proxy-type", "Specifies the proxy type, 'http' (default), 'none' (disable completely), or 'socks5'", QCommandLine::Optional },
|
||||
{ QCommandLine::Option, '\0', "script-encoding", "Sets the encoding used for the starting script, default is 'utf8'", QCommandLine::Optional },
|
||||
{ QCommandLine::Option, '\0', "script-language", "Sets the script language instead of detecting it: 'javascript', 'coffeescript'", QCommandLine::Optional },
|
||||
{ QCommandLine::Option, '\0', "web-security", "Enables web security, 'true' (default) or 'false'", QCommandLine::Optional },
|
||||
{ QCommandLine::Option, '\0', "ssl-protocol", "Sets the SSL protocol (supported protocols: 'SSLv3' (default), 'SSLv2', 'TLSv1', 'any')", QCommandLine::Optional },
|
||||
{ QCommandLine::Option, '\0', "ssl-certificates-path", "Sets the location for custom CA certificates (if none set, uses system default)", QCommandLine::Optional },
|
||||
|
@ -364,6 +365,20 @@ void Config::setScriptEncoding(const QString &value)
|
|||
m_scriptEncoding = value;
|
||||
}
|
||||
|
||||
QString Config::scriptLanguage() const
|
||||
{
|
||||
return m_scriptLanguage;
|
||||
}
|
||||
|
||||
void Config::setScriptLanguage(const QString &value)
|
||||
{
|
||||
if (value.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_scriptLanguage = value;
|
||||
}
|
||||
|
||||
QString Config::scriptFile() const
|
||||
{
|
||||
return m_scriptFile;
|
||||
|
@ -528,6 +543,7 @@ void Config::resetToDefaults()
|
|||
m_proxyAuthPass.clear();
|
||||
m_scriptArgs.clear();
|
||||
m_scriptEncoding = "UTF-8";
|
||||
m_scriptLanguage.clear();
|
||||
m_scriptFile.clear();
|
||||
m_unknownOption.clear();
|
||||
m_versionFlag = false;
|
||||
|
@ -687,6 +703,10 @@ void Config::handleOption(const QString &option, const QVariant &value)
|
|||
setScriptEncoding(value.toString());
|
||||
}
|
||||
|
||||
if (option == "script-language") {
|
||||
setScriptLanguage(value.toString());
|
||||
}
|
||||
|
||||
if (option == "web-security") {
|
||||
setWebSecurityEnabled(boolValue);
|
||||
}
|
||||
|
|
|
@ -121,6 +121,9 @@ public:
|
|||
QString scriptEncoding() const;
|
||||
void setScriptEncoding(const QString &value);
|
||||
|
||||
QString scriptLanguage() const;
|
||||
void setScriptLanguage(const QString &value);
|
||||
|
||||
QString scriptFile() const;
|
||||
void setScriptFile(const QString &value);
|
||||
|
||||
|
@ -203,6 +206,7 @@ private:
|
|||
QString m_proxyAuthPass;
|
||||
QStringList m_scriptArgs;
|
||||
QString m_scriptEncoding;
|
||||
QString m_scriptLanguage;
|
||||
QString m_scriptFile;
|
||||
QString m_unknownOption;
|
||||
bool m_versionFlag;
|
||||
|
|
|
@ -205,7 +205,7 @@ bool Phantom::execute()
|
|||
qDebug() << "Phantom - execute: Starting Remote WebDriver mode";
|
||||
|
||||
Terminal::instance()->cout("PhantomJS is launching GhostDriver...");
|
||||
if (!Utils::injectJsInFrame(":/ghostdriver/main.js", m_scriptFileEnc, QDir::currentPath(), m_page->mainFrame(), true)) {
|
||||
if (!Utils::injectJsInFrame(":/ghostdriver/main.js", QString(), m_scriptFileEnc, QDir::currentPath(), m_page->mainFrame(), true)) {
|
||||
m_returnValue = -1;
|
||||
return false;
|
||||
}
|
||||
|
@ -225,7 +225,7 @@ bool Phantom::execute()
|
|||
}
|
||||
m_page->showInspector(m_config.remoteDebugPort());
|
||||
} else {
|
||||
if (!Utils::injectJsInFrame(m_config.scriptFile(), m_scriptFileEnc, QDir::currentPath(), m_page->mainFrame(), true)) {
|
||||
if (!Utils::injectJsInFrame(m_config.scriptFile(), m_config.scriptLanguage(), m_scriptFileEnc, QDir::currentPath(), m_page->mainFrame(), true)) {
|
||||
m_returnValue = -1;
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -101,14 +101,14 @@ QVariant Utils::coffee2js(const QString &script)
|
|||
|
||||
bool Utils::injectJsInFrame(const QString &jsFilePath, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript)
|
||||
{
|
||||
return injectJsInFrame(jsFilePath, Encoding::UTF8, libraryPath, targetFrame, startingScript);
|
||||
return injectJsInFrame(jsFilePath, QString(), Encoding::UTF8, libraryPath, targetFrame, startingScript);
|
||||
}
|
||||
|
||||
bool Utils::injectJsInFrame(const QString &jsFilePath, const Encoding &jsFileEnc, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript)
|
||||
bool Utils::injectJsInFrame(const QString &jsFilePath, const QString &jsFileLanguage, const Encoding &jsFileEnc, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript)
|
||||
{
|
||||
// Don't do anything if an empty string is passed
|
||||
QString scriptPath = findScript(jsFilePath, libraryPath);
|
||||
QString scriptBody = jsFromScriptFile(scriptPath, jsFileEnc);
|
||||
QString scriptBody = jsFromScriptFile(scriptPath, jsFileLanguage, jsFileEnc);
|
||||
if (scriptBody.isEmpty())
|
||||
{
|
||||
if (startingScript) {
|
||||
|
@ -131,7 +131,7 @@ bool Utils::loadJSForDebug(const QString& jsFilePath, const QString& libraryPath
|
|||
bool Utils::loadJSForDebug(const QString& jsFilePath, const Encoding& jsFileEnc, const QString& libraryPath, QWebFrame* targetFrame, const bool autorun)
|
||||
{
|
||||
QString scriptPath = findScript(jsFilePath, libraryPath);
|
||||
QString scriptBody = jsFromScriptFile(scriptPath, jsFileEnc);
|
||||
QString scriptBody = jsFromScriptFile(scriptPath, QString(), jsFileEnc);
|
||||
|
||||
QString remoteDebuggerHarnessSrc = Utils::readResourceFileUtf8(":/remote_debugger_harness.html");
|
||||
remoteDebuggerHarnessSrc = remoteDebuggerHarnessSrc.arg(scriptBody);
|
||||
|
@ -162,17 +162,22 @@ QString Utils::findScript(const QString& jsFilePath, const QString &libraryPath)
|
|||
return QString();
|
||||
}
|
||||
|
||||
QString Utils::jsFromScriptFile(const QString& scriptPath, const Encoding& enc)
|
||||
QString Utils::jsFromScriptFile(const QString& scriptPath, const QString& scriptLanguage, const Encoding& enc)
|
||||
{
|
||||
QFile jsFile(scriptPath);
|
||||
if (jsFile.exists() && jsFile.open(QFile::ReadOnly)) {
|
||||
QString scriptBody = enc.decode(jsFile.readAll());
|
||||
|
||||
// Remove CLI script heading
|
||||
if (scriptBody.startsWith("#!") && !jsFile.fileName().endsWith(COFFEE_SCRIPT_EXTENSION)) {
|
||||
scriptBody.prepend("//");
|
||||
if (scriptBody.startsWith("#!")) {
|
||||
int len = scriptBody.indexOf(QRegExp("[\r\n]"));
|
||||
if (len == -1) len = scriptBody.length();
|
||||
scriptBody.remove(0, len);
|
||||
}
|
||||
|
||||
if (jsFile.fileName().endsWith(COFFEE_SCRIPT_EXTENSION)) {
|
||||
// If the language is set to coffeescript, or the language is not set and the file ends in .coffee, make coffee.
|
||||
if (scriptLanguage == "coffeescript" ||
|
||||
(scriptLanguage.isNull() && jsFile.fileName().endsWith(COFFEE_SCRIPT_EXTENSION))) {
|
||||
QVariant result = Utils::coffee2js(scriptBody);
|
||||
if (result.toStringList().at(0) == "false") {
|
||||
return QString();
|
||||
|
@ -180,6 +185,7 @@ QString Utils::jsFromScriptFile(const QString& scriptPath, const Encoding& enc)
|
|||
scriptBody = result.toStringList().at(1);
|
||||
}
|
||||
}
|
||||
|
||||
jsFile.close();
|
||||
|
||||
return scriptBody;
|
||||
|
|
|
@ -59,7 +59,7 @@ public:
|
|||
#endif
|
||||
static QVariant coffee2js(const QString &script);
|
||||
static bool injectJsInFrame(const QString &jsFilePath, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript = false);
|
||||
static bool injectJsInFrame(const QString &jsFilePath, const Encoding &jsFileEnc, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript = false);
|
||||
static bool injectJsInFrame(const QString &jsFilePath, const QString &jsFileLanguage, const Encoding &jsFileEnc, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript = false);
|
||||
static QString readResourceFileUtf8(const QString &resourceFilePath);
|
||||
|
||||
static bool loadJSForDebug(const QString &jsFilePath, const Encoding &jsFileEnc, const QString &libraryPath, QWebFrame *targetFrame, const bool autorun = false);
|
||||
|
@ -70,7 +70,7 @@ public:
|
|||
|
||||
private:
|
||||
static QString findScript(const QString &jsFilePath, const QString& libraryPath);
|
||||
static QString jsFromScriptFile(const QString& scriptPath, const Encoding& enc);
|
||||
static QString jsFromScriptFile(const QString& scriptPath, const QString& lang, const Encoding& enc);
|
||||
Utils(); //< This class shouldn't be instantiated
|
||||
|
||||
static QTemporaryFile* m_tempHarness; //< We want to make sure to clean up after ourselves
|
||||
|
|
Loading…
Reference in New Issue