2012-07-11 18:30:00 +02:00
|
|
|
/* ============================================================
|
|
|
|
* GreaseMonkey plugin for QupZilla
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2012-2014 David Rosca <nowrep@gmail.com>
|
2012-07-11 18:30:00 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "gm_script.h"
|
|
|
|
#include "gm_manager.h"
|
2013-04-02 13:14:19 +02:00
|
|
|
|
2013-04-02 11:21:12 +02:00
|
|
|
#include "qzregexp.h"
|
2013-04-02 13:14:19 +02:00
|
|
|
#include "delayedfilewatcher.h"
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QWebFrame>
|
|
|
|
#include <QDebug>
|
2013-04-02 11:21:12 +02:00
|
|
|
#include <QCryptographicHash>
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
GM_Script::GM_Script(GM_Manager* manager, const QString &filePath)
|
2013-01-30 14:12:09 +01:00
|
|
|
: QObject(manager)
|
|
|
|
, m_manager(manager)
|
2013-04-02 13:14:19 +02:00
|
|
|
, m_fileWatcher(new DelayedFileWatcher(this))
|
2013-04-02 11:57:31 +02:00
|
|
|
, m_namespace("GreaseMonkeyNS")
|
|
|
|
, m_startAt(DocumentEnd)
|
2012-07-11 18:30:00 +02:00
|
|
|
, m_fileName(filePath)
|
2013-04-02 11:57:31 +02:00
|
|
|
, m_enabled(true)
|
|
|
|
, m_valid(false)
|
2012-07-11 18:30:00 +02:00
|
|
|
{
|
2013-01-30 14:12:09 +01:00
|
|
|
parseScript();
|
|
|
|
|
2013-04-02 13:14:19 +02:00
|
|
|
connect(m_fileWatcher, SIGNAL(delayedFileChanged(QString)), this, SLOT(watchedFileChanged(QString)));
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GM_Script::isValid() const
|
|
|
|
{
|
|
|
|
return m_valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Script::name() const
|
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Script::nameSpace() const
|
|
|
|
{
|
|
|
|
return m_namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Script::fullName() const
|
|
|
|
{
|
|
|
|
return QString("%1/%2").arg(m_namespace, m_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Script::description() const
|
|
|
|
{
|
|
|
|
return m_description;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Script::version() const
|
|
|
|
{
|
|
|
|
return m_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl GM_Script::downloadUrl() const
|
|
|
|
{
|
|
|
|
return m_downloadUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
GM_Script::StartAt GM_Script::startAt() const
|
|
|
|
{
|
|
|
|
return m_startAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GM_Script::isEnabled() const
|
|
|
|
{
|
2013-01-30 14:12:09 +01:00
|
|
|
return m_valid && m_enabled;
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Script::setEnabled(bool enable)
|
|
|
|
{
|
|
|
|
m_enabled = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList GM_Script::include() const
|
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const GM_UrlMatcher &matcher, m_include) {
|
2012-07-11 18:30:00 +02:00
|
|
|
list.append(matcher.pattern());
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList GM_Script::exclude() const
|
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const GM_UrlMatcher &matcher, m_exclude) {
|
2012-07-11 18:30:00 +02:00
|
|
|
list.append(matcher.pattern());
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Script::script() const
|
|
|
|
{
|
|
|
|
return m_script;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Script::fileName() const
|
|
|
|
{
|
|
|
|
return m_fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GM_Script::match(const QString &urlString)
|
|
|
|
{
|
2013-01-30 14:12:09 +01:00
|
|
|
if (!isEnabled()) {
|
2012-07-11 18:30:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const GM_UrlMatcher &matcher, m_exclude) {
|
2012-07-11 18:30:00 +02:00
|
|
|
if (matcher.match(urlString)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const GM_UrlMatcher &matcher, m_include) {
|
2012-07-11 18:30:00 +02:00
|
|
|
if (matcher.match(urlString)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-30 14:12:09 +01:00
|
|
|
void GM_Script::watchedFileChanged(const QString &file)
|
2012-07-11 18:30:00 +02:00
|
|
|
{
|
2013-01-30 14:12:09 +01:00
|
|
|
if (m_fileName == file) {
|
|
|
|
parseScript();
|
|
|
|
|
|
|
|
m_manager->removeScript(this, false);
|
|
|
|
m_manager->addScript(this);
|
2013-06-02 18:25:25 +02:00
|
|
|
|
|
|
|
emit scriptChanged();
|
2013-01-30 14:12:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Script::parseScript()
|
|
|
|
{
|
2013-04-02 11:57:31 +02:00
|
|
|
QFile file(m_fileName);
|
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
|
|
|
qWarning() << "GreaseMonkey: Cannot open file for reading" << m_fileName;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-30 14:12:09 +01:00
|
|
|
m_name.clear();
|
|
|
|
m_namespace = "GreaseMonkeyNS";
|
|
|
|
m_description.clear();
|
|
|
|
m_version.clear();
|
|
|
|
m_include.clear();
|
|
|
|
m_exclude.clear();
|
|
|
|
m_downloadUrl.clear();
|
|
|
|
m_startAt = DocumentEnd;
|
|
|
|
m_enabled = true;
|
|
|
|
m_valid = false;
|
|
|
|
|
|
|
|
if (!m_fileWatcher->files().contains(m_fileName)) {
|
|
|
|
m_fileWatcher->addPath(m_fileName);
|
|
|
|
}
|
|
|
|
|
2012-07-11 18:30:00 +02:00
|
|
|
QString fileData = QString::fromUtf8(file.readAll());
|
|
|
|
|
2013-02-24 10:57:58 +01:00
|
|
|
QzRegExp rx("// ==UserScript==(.*)// ==/UserScript==");
|
2012-07-11 18:30:00 +02:00
|
|
|
rx.indexIn(fileData);
|
|
|
|
QString metadataBlock = rx.cap(1).trimmed();
|
|
|
|
|
|
|
|
if (metadataBlock.isEmpty()) {
|
2013-01-30 14:12:09 +01:00
|
|
|
qWarning() << "GreaseMonkey: File does not contain metadata block" << m_fileName;
|
2012-07-11 18:30:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList requireList;
|
|
|
|
|
2013-12-30 13:43:48 +01:00
|
|
|
const QStringList lines = metadataBlock.split(QLatin1Char('\n'), QString::SkipEmptyParts);
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (QString line, lines) {
|
2012-09-04 11:29:47 +02:00
|
|
|
if (!line.startsWith(QLatin1String("// @"))) {
|
2012-07-11 18:30:00 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-09-04 11:29:47 +02:00
|
|
|
line = line.mid(3).replace(QLatin1Char('\t'), QLatin1Char(' '));
|
|
|
|
int index = line.indexOf(QLatin1Char(' '));
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString key = line.left(index).trimmed();
|
|
|
|
const QString value = line.mid(index + 1).trimmed();
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
// Ignored values:
|
|
|
|
// @resource
|
|
|
|
// @unwrap
|
|
|
|
|
|
|
|
if (key.isEmpty() || value.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-09-04 11:29:47 +02:00
|
|
|
if (key == QLatin1String("@name")) {
|
2012-07-11 18:30:00 +02:00
|
|
|
m_name = value;
|
|
|
|
}
|
2012-09-04 11:29:47 +02:00
|
|
|
else if (key == QLatin1String("@namespace")) {
|
2012-07-11 18:30:00 +02:00
|
|
|
m_namespace = value;
|
|
|
|
}
|
2012-09-04 11:29:47 +02:00
|
|
|
else if (key == QLatin1String("@description")) {
|
2012-07-11 18:30:00 +02:00
|
|
|
m_description = value;
|
|
|
|
}
|
2012-09-04 11:29:47 +02:00
|
|
|
else if (key == QLatin1String("@version")) {
|
2012-07-11 18:30:00 +02:00
|
|
|
m_version = value;
|
|
|
|
}
|
2012-09-04 11:29:47 +02:00
|
|
|
else if (key == QLatin1String("@updateURL")) {
|
2012-07-11 18:30:00 +02:00
|
|
|
m_downloadUrl = QUrl(value);
|
|
|
|
}
|
2012-09-04 11:29:47 +02:00
|
|
|
else if (key == QLatin1String("@include") || key == QLatin1String("@match")) {
|
2012-07-11 18:30:00 +02:00
|
|
|
m_include.append(GM_UrlMatcher(value));
|
|
|
|
}
|
2012-09-04 11:29:47 +02:00
|
|
|
else if (key == QLatin1String("@exclude") || key == QLatin1String("@exclude_match")) {
|
2012-07-11 18:30:00 +02:00
|
|
|
m_exclude.append(GM_UrlMatcher(value));
|
|
|
|
}
|
2012-09-04 11:29:47 +02:00
|
|
|
else if (key == QLatin1String("@require")) {
|
2012-07-11 18:30:00 +02:00
|
|
|
requireList.append(value);
|
|
|
|
}
|
2012-09-04 11:29:47 +02:00
|
|
|
else if (key == QLatin1String("@run-at")) {
|
|
|
|
if (value == QLatin1String("document-end")) {
|
2012-07-11 18:30:00 +02:00
|
|
|
m_startAt = DocumentEnd;
|
|
|
|
}
|
2012-09-04 11:29:47 +02:00
|
|
|
else if (value == QLatin1String("document-start")) {
|
2012-07-11 18:30:00 +02:00
|
|
|
m_startAt = DocumentStart;
|
|
|
|
}
|
|
|
|
}
|
2012-09-04 11:29:47 +02:00
|
|
|
else if (key == QLatin1String("@downloadURL") && m_downloadUrl.isEmpty()) {
|
2012-07-11 18:30:00 +02:00
|
|
|
m_downloadUrl = QUrl(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_include.isEmpty()) {
|
|
|
|
m_include.append(GM_UrlMatcher("*"));
|
|
|
|
}
|
|
|
|
|
2012-09-04 11:29:47 +02:00
|
|
|
int index = fileData.indexOf(QLatin1String("// ==/UserScript==")) + 18;
|
2012-07-11 18:30:00 +02:00
|
|
|
QString script = fileData.mid(index).trimmed();
|
|
|
|
|
2013-04-02 11:21:12 +02:00
|
|
|
QString jscript("(function(){"
|
|
|
|
"function GM_getValue(name,val){return GM_getValueImpl('%1',name,val);}"
|
|
|
|
"function GM_setValue(name,val){return GM_setValueImpl('%1',name,val);}"
|
|
|
|
"function GM_deleteValue(name){return GM_deleteValueImpl('%1',name);}"
|
|
|
|
"function GM_listValues(){return GM_listValuesImpl('%1');}"
|
|
|
|
"\n%2\n})();");
|
|
|
|
QString nspace = QCryptographicHash::hash(fullName().toUtf8(), QCryptographicHash::Md4).toHex();
|
|
|
|
|
2012-07-11 18:30:00 +02:00
|
|
|
script.prepend(m_manager->requireScripts(requireList));
|
2013-04-02 11:21:12 +02:00
|
|
|
script = jscript.arg(nspace, script);
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
m_script = script;
|
|
|
|
m_valid = !script.isEmpty();
|
|
|
|
}
|