Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Remove code to deal with non-JavaScript support (#15401)
Browse files Browse the repository at this point in the history
This was necessary long time ago when CoffeeScript was still supported.
Now it's time to get rid of the crufts.
  • Loading branch information
ariya committed Dec 28, 2019
1 parent 5da1448 commit 42ce79b
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 70 deletions.
19 changes: 0 additions & 19 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,20 +388,6 @@ 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;
Expand Down Expand Up @@ -516,7 +502,6 @@ 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;
Expand Down Expand Up @@ -706,10 +691,6 @@ 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);
}
Expand Down
3 changes: 0 additions & 3 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ class Config: public QObject
QString scriptEncoding() const;
void setScriptEncoding(const QString& value);

QString scriptLanguage() const;
void setScriptLanguage(const QString& value);

QString scriptFile() const;
void setScriptFile(const QString& value);

Expand Down
15 changes: 3 additions & 12 deletions src/phantom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,6 @@ bool Phantom::execute()
if (m_config.scriptFile().isEmpty()) { // REPL mode requested
qDebug() << "Phantom - execute: Starting REPL mode";

// REPL is only valid for javascript
const QString& scriptLanguage = m_config.scriptLanguage();
if (scriptLanguage != "javascript" && !scriptLanguage.isNull()) {
QString errMessage = QString("Unsupported language: %1").arg(scriptLanguage);
Terminal::instance()->cerr(errMessage);
qWarning("%s", qPrintable(errMessage));
return false;
}

// Create the REPL: it will launch itself, no need to store this variable.
REPL::getInstance(m_page->mainFrame(), this);
} else { // Load the User Script
Expand All @@ -229,12 +220,12 @@ bool Phantom::execute()
if (m_config.remoteDebugPort() == 0) {
qWarning() << "Can't bind remote debugging server to the port" << originalPort;
}
if (!Utils::loadJSForDebug(m_config.scriptFile(), m_config.scriptLanguage(), m_scriptFileEnc, QDir::currentPath(), m_page->mainFrame(), m_config.remoteDebugAutorun())) {
if (!Utils::loadJSForDebug(m_config.scriptFile(), m_scriptFileEnc, QDir::currentPath(), m_page->mainFrame(), m_config.remoteDebugAutorun())) {
m_returnValue = -1;
return false;
}
} else {
if (!Utils::injectJsInFrame(m_config.scriptFile(), m_config.scriptLanguage(), m_scriptFileEnc, QDir::currentPath(), m_page->mainFrame(), true)) {
if (!Utils::injectJsInFrame(m_config.scriptFile(), m_scriptFileEnc, QDir::currentPath(), m_page->mainFrame(), true)) {
m_returnValue = -1;
return false;
}
Expand Down Expand Up @@ -391,7 +382,7 @@ bool Phantom::injectJs(const QString& jsFilePath)
return false;
}

return Utils::injectJsInFrame(pre + jsFilePath, libraryPath(), m_page->mainFrame());
return Utils::injectJsInFrame(pre + jsFilePath, Encoding::UTF8, libraryPath(), m_page->mainFrame());
}

void Phantom::setProxy(const QString& ip, const qint64& port, const QString& proxyType, const QString& user, const QString& password)
Expand Down
28 changes: 5 additions & 23 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static QString findScript(const QString& jsFilePath, const QString& libraryPath)
return QString();
}

static QString jsFromScriptFile(const QString& scriptPath, const QString& scriptLanguage, const Encoding& enc)
static QString jsFromScriptFile(const QString& scriptPath, const Encoding& enc)
{
QFile jsFile(scriptPath);
if (jsFile.exists() && jsFile.open(QFile::ReadOnly)) {
Expand All @@ -69,14 +69,6 @@ static QString jsFromScriptFile(const QString& scriptPath, const QString& script
scriptBody.remove(0, len);
}

// If a language is specified and is not "javascript", reject it.
if (scriptLanguage != "javascript" && !scriptLanguage.isNull()) {
QString errMessage = QString("Unsupported language: %1").arg(scriptLanguage);
Terminal::instance()->cerr(errMessage);
qWarning("%s", qPrintable(errMessage));
return QString();
}

return scriptBody;
} else {
return QString();
Expand Down Expand Up @@ -113,16 +105,11 @@ void messageHandler(QtMsgType type, const QMessageLogContext& context, const QSt
}
}

bool injectJsInFrame(const QString& jsFilePath, const QString& libraryPath, QWebFrame* targetFrame, const bool startingScript)
{
return injectJsInFrame(jsFilePath, QString(), Encoding::UTF8, libraryPath, targetFrame, startingScript);
}

bool injectJsInFrame(const QString& jsFilePath, const QString& jsFileLanguage, const Encoding& jsFileEnc, const QString& libraryPath, QWebFrame* targetFrame, const bool startingScript)
bool injectJsInFrame(const QString& jsFilePath, 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, jsFileLanguage, jsFileEnc);
QString scriptBody = jsFromScriptFile(scriptPath, jsFileEnc);
if (scriptBody.isEmpty()) {
if (startingScript) {
Terminal::instance()->cerr(QString("Can't open '%1'").arg(jsFilePath));
Expand All @@ -136,15 +123,10 @@ bool injectJsInFrame(const QString& jsFilePath, const QString& jsFileLanguage, c
return true;
}

bool loadJSForDebug(const QString& jsFilePath, const QString& libraryPath, QWebFrame* targetFrame, const bool autorun)
{
return loadJSForDebug(jsFilePath, QString(), Encoding::UTF8, libraryPath, targetFrame, autorun);
}

bool loadJSForDebug(const QString& jsFilePath, const QString& jsFileLanguage, const Encoding& jsFileEnc, const QString& libraryPath, QWebFrame* targetFrame, const bool autorun)
bool loadJSForDebug(const QString& jsFilePath, const Encoding& jsFileEnc, const QString& libraryPath, QWebFrame* targetFrame, const bool autorun)
{
QString scriptPath = findScript(jsFilePath, libraryPath);
QString scriptBody = jsFromScriptFile(scriptPath, jsFileLanguage, jsFileEnc);
QString scriptBody = jsFromScriptFile(scriptPath, jsFileEnc);

scriptBody = QString("function __run() {\n%1\n}").arg(scriptBody);
targetFrame->evaluateJavaScript(scriptBody);
Expand Down
12 changes: 0 additions & 12 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,12 @@ void messageHandler(QtMsgType type,
extern bool printDebugMessages;

bool injectJsInFrame(const QString& jsFilePath,
const QString& libraryPath,
QWebFrame* targetFrame,
const bool startingScript = false);

bool injectJsInFrame(const QString& jsFilePath,
const QString& jsFileLanguage,
const Encoding& jsFileEnc,
const QString& libraryPath,
QWebFrame* targetFrame,
const bool startingScript = false);

bool loadJSForDebug(const QString& jsFilePath,
const QString& libraryPath,
QWebFrame* targetFrame,
const bool autorun = false);

bool loadJSForDebug(const QString& jsFilePath,
const QString& jsFileLanguage,
const Encoding& jsFileEnc,
const QString& libraryPath,
QWebFrame* targetFrame,
Expand Down
2 changes: 1 addition & 1 deletion src/webpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,7 @@ void WebPage::_uploadFile(const QString& selector, const QStringList& fileNames)

bool WebPage::injectJs(const QString& jsFilePath)
{
return Utils::injectJsInFrame(jsFilePath, m_libraryPath, m_currentFrame);
return Utils::injectJsInFrame(jsFilePath, Encoding::UTF8, m_libraryPath, m_currentFrame);
}

void WebPage::_appendScriptElement(const QString& scriptUrl)
Expand Down

0 comments on commit 42ce79b

Please sign in to comment.