2012-07-11 18:30:00 +02:00
|
|
|
/* ============================================================
|
|
|
|
* GreaseMonkey plugin for QupZilla
|
2013-01-30 14:12:09 +01:00
|
|
|
* Copyright (C) 2012-2013 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_manager.h"
|
|
|
|
#include "gm_script.h"
|
|
|
|
#include "gm_downloader.h"
|
2013-04-02 11:21:12 +02:00
|
|
|
#include "gm_jsobject.h"
|
2012-07-11 18:30:00 +02:00
|
|
|
#include "settings/gm_settings.h"
|
|
|
|
|
|
|
|
#include "webpage.h"
|
2013-01-22 19:04:22 +01:00
|
|
|
#include "qztools.h"
|
2012-07-11 18:30:00 +02:00
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "desktopnotificationsfactory.h"
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QWebFrame>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
GM_Manager::GM_Manager(const QString &sPath, QObject* parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, m_settingsPath(sPath)
|
2013-04-02 11:21:12 +02:00
|
|
|
, m_jsObject(new GM_JSObject(this))
|
2012-07-11 18:30:00 +02:00
|
|
|
{
|
|
|
|
QTimer::singleShot(0, this, SLOT(load()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Manager::showSettings(QWidget* parent)
|
|
|
|
{
|
2012-09-11 11:31:28 +02:00
|
|
|
if (!m_settings) {
|
|
|
|
m_settings = new GM_Settings(this, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_settings.data()->show();
|
|
|
|
m_settings.data()->raise();
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Manager::downloadScript(const QNetworkRequest &request)
|
|
|
|
{
|
|
|
|
new GM_Downloader(request, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Manager::settinsPath() const
|
|
|
|
{
|
|
|
|
return m_settingsPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Manager::scriptsDirectory() const
|
|
|
|
{
|
|
|
|
return m_settingsPath + "/greasemonkey/";
|
|
|
|
}
|
|
|
|
|
|
|
|
QString GM_Manager::requireScripts(const QStringList &urlList) const
|
|
|
|
{
|
|
|
|
QDir requiresDir(m_settingsPath + "greasemonkey/requires");
|
|
|
|
if (!requiresDir.exists() || urlList.isEmpty()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSettings settings(m_settingsPath + "greasemonkey/requires/requires.ini", QSettings::IniFormat);
|
|
|
|
settings.beginGroup("Files");
|
|
|
|
|
|
|
|
QString script;
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QString &url, urlList) {
|
2012-07-11 18:30:00 +02:00
|
|
|
if (settings.contains(url)) {
|
|
|
|
const QString &fileName = settings.value(url).toString();
|
2013-01-22 19:04:22 +01:00
|
|
|
script.append(QzTools::readAllFileContents(fileName).trimmed() + '\n');
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2012-09-11 11:31:28 +02:00
|
|
|
void GM_Manager::unloadPlugin()
|
2012-07-11 18:30:00 +02:00
|
|
|
{
|
2012-09-11 11:31:28 +02:00
|
|
|
// Save settings
|
2012-07-11 18:30:00 +02:00
|
|
|
QSettings settings(m_settingsPath + "extensions.ini", QSettings::IniFormat);
|
|
|
|
settings.beginGroup("GreaseMonkey");
|
|
|
|
settings.setValue("disabledScripts", m_disabledScripts);
|
2012-09-11 11:31:28 +02:00
|
|
|
settings.endGroup();
|
|
|
|
|
|
|
|
delete m_settings.data();
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<GM_Script*> GM_Manager::allScripts() const
|
|
|
|
{
|
|
|
|
QList<GM_Script*> list;
|
|
|
|
list.append(m_startScripts);
|
|
|
|
list.append(m_endScripts);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GM_Manager::containsScript(const QString &fullName) const
|
|
|
|
{
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (GM_Script* script, m_startScripts) {
|
2012-07-11 18:30:00 +02:00
|
|
|
if (fullName == script->fullName()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (GM_Script* script, m_endScripts) {
|
2012-07-11 18:30:00 +02:00
|
|
|
if (fullName == script->fullName()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Manager::enableScript(GM_Script* script)
|
|
|
|
{
|
|
|
|
script->setEnabled(true);
|
|
|
|
m_disabledScripts.removeOne(script->fullName());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Manager::disableScript(GM_Script* script)
|
|
|
|
{
|
|
|
|
script->setEnabled(false);
|
|
|
|
m_disabledScripts.append(script->fullName());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GM_Manager::addScript(GM_Script* script)
|
|
|
|
{
|
2013-04-02 11:57:31 +02:00
|
|
|
if (!script || !script->isValid()) {
|
2012-07-11 18:30:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (script->startAt() == GM_Script::DocumentStart) {
|
|
|
|
m_startScripts.append(script);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_endScripts.append(script);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit scriptsChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-30 14:12:09 +01:00
|
|
|
bool GM_Manager::removeScript(GM_Script* script, bool removeFile)
|
2012-07-11 18:30:00 +02:00
|
|
|
{
|
|
|
|
if (!script) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (script->startAt() == GM_Script::DocumentStart) {
|
|
|
|
m_startScripts.removeOne(script);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_endScripts.removeOne(script);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_disabledScripts.removeOne(script->fullName());
|
2013-01-30 14:12:09 +01:00
|
|
|
|
|
|
|
if (removeFile) {
|
|
|
|
QFile::remove(script->fileName());
|
|
|
|
delete script;
|
|
|
|
}
|
2012-07-11 18:30:00 +02:00
|
|
|
|
|
|
|
emit scriptsChanged();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Manager::showNotification(const QString &message, const QString &title)
|
|
|
|
{
|
|
|
|
QPixmap icon(":gm/data/icon.png");
|
|
|
|
|
|
|
|
mApp->desktopNotifications()->showNotification(icon, title.isEmpty() ? tr("GreaseMonkey") : title, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Manager::pageLoadStart()
|
|
|
|
{
|
|
|
|
QWebFrame* frame = qobject_cast<QWebFrame*>(sender());
|
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &urlScheme = frame->url().scheme();
|
|
|
|
const QString &urlString = frame->url().toEncoded();
|
|
|
|
|
|
|
|
if (!canRunOnScheme(urlScheme)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-02 11:21:12 +02:00
|
|
|
frame->addToJavaScriptWindowObject("_qz_greasemonkey", m_jsObject);
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (GM_Script* script, m_startScripts) {
|
2012-07-11 18:30:00 +02:00
|
|
|
if (script->match(urlString)) {
|
|
|
|
frame->evaluateJavaScript(m_bootstrap + script->script());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (GM_Script* script, m_endScripts) {
|
2012-07-11 18:30:00 +02:00
|
|
|
if (script->match(urlString)) {
|
|
|
|
const QString &jscript = QString("window.addEventListener(\"DOMContentLoaded\","
|
2013-04-02 11:21:12 +02:00
|
|
|
"function(e) { \n%1\n }, false);").arg(m_bootstrap + script->script());
|
2012-07-11 18:30:00 +02:00
|
|
|
frame->evaluateJavaScript(jscript);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GM_Manager::load()
|
|
|
|
{
|
|
|
|
QDir gmDir(m_settingsPath + "greasemonkey");
|
|
|
|
if (!gmDir.exists()) {
|
|
|
|
gmDir.mkdir(m_settingsPath + "greasemonkey");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gmDir.exists("requires")) {
|
|
|
|
gmDir.mkdir("requires");
|
|
|
|
}
|
|
|
|
|
|
|
|
QSettings settings(m_settingsPath + "extensions.ini", QSettings::IniFormat);
|
|
|
|
settings.beginGroup("GreaseMonkey");
|
|
|
|
m_disabledScripts = settings.value("disabledScripts", QStringList()).toStringList();
|
|
|
|
|
2013-03-06 09:05:41 +01:00
|
|
|
foreach (const QString &fileName, gmDir.entryList(QStringList("*.js"), QDir::Files)) {
|
2012-07-11 18:30:00 +02:00
|
|
|
const QString &absolutePath = gmDir.absoluteFilePath(fileName);
|
|
|
|
GM_Script* script = new GM_Script(this, absolutePath);
|
|
|
|
|
2013-02-24 10:48:23 +01:00
|
|
|
if (!script->isValid()) {
|
|
|
|
delete script;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-07-11 18:30:00 +02:00
|
|
|
if (m_disabledScripts.contains(script->fullName())) {
|
|
|
|
script->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (script->startAt() == GM_Script::DocumentStart) {
|
|
|
|
m_startScripts.append(script);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_endScripts.append(script);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 19:04:22 +01:00
|
|
|
m_bootstrap = QzTools::readAllFileContents(":gm/data/bootstrap.min.js");
|
2013-04-02 11:21:12 +02:00
|
|
|
m_jsObject->setSettingsFile(m_settingsPath + "extensions.ini");
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GM_Manager::canRunOnScheme(const QString &scheme)
|
|
|
|
{
|
2012-09-04 11:29:47 +02:00
|
|
|
return (scheme == QLatin1String("http") || scheme == QLatin1String("https")
|
|
|
|
|| scheme == QLatin1String("data") || scheme == QLatin1String("ftp"));
|
2012-07-11 18:30:00 +02:00
|
|
|
}
|