eric6/WebBrowser/GreaseMonkey/GreaseMonkeyManager.py

Sun, 14 Apr 2019 15:09:21 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 14 Apr 2019 15:09:21 +0200
changeset 6942
2602857055c5
parent 6645
WebBrowser/GreaseMonkey/GreaseMonkeyManager.py@ad476851d7e0
child 7229
53054eb5b15a
permissions
-rw-r--r--

Major restructuring of the source tree to get prepared for a setup.py based installation.

1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
6645
ad476851d7e0 Updated copyright for 2019.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6128
diff changeset
3 # Copyright (c) 2012 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the manager for GreaseMonkey scripts.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
3145
a9de05d4a22f # __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 3060
diff changeset
10 from __future__ import unicode_literals
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2403
diff changeset
11
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import os
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
4913
e16573640cb8 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4905
diff changeset
14 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QObject, QTimer, QFile, \
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
15 QFileInfo, QDir, QSettings, QMetaObject, QUrl, Q_ARG, QCoreApplication
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
16 from PyQt5.QtWidgets import QDialog
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
17
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
18 from E5Gui import E5MessageBox
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 import Utilities
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 import Preferences
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
23 from WebBrowser.WebBrowserWindow import WebBrowserWindow
6128
afc2cda1a743 Implemented support for GreaseMonkey 4.0 for the web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6120
diff changeset
24 from WebBrowser.JavaScript.ExternalJsObject import ExternalJsObject
afc2cda1a743 Implemented support for GreaseMonkey 4.0 for the web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6120
diff changeset
25
afc2cda1a743 Implemented support for GreaseMonkey 4.0 for the web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6120
diff changeset
26 from .GreaseMonkeyJsObject import GreaseMonkeyJsObject
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
27
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 class GreaseMonkeyManager(QObject):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 Class implementing the manager for GreaseMonkey scripts.
5726
e1dbd217214a Fixed a few source docu issues (forgotten signals and exceptions).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5716
diff changeset
32
e1dbd217214a Fixed a few source docu issues (forgotten signals and exceptions).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5716
diff changeset
33 @signal scriptsChanged() emitted to indicate a change of scripts
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 scriptsChanged = pyqtSignal()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 def __init__(self, parent=None):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 Constructor
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 @param parent reference to the parent object (QObject)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2403
diff changeset
43 super(GreaseMonkeyManager, self).__init__(parent)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 self.__disabledScripts = []
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
46 self.__scripts = []
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 self.__downloaders = []
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48
6128
afc2cda1a743 Implemented support for GreaseMonkey 4.0 for the web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6120
diff changeset
49 self.__jsObject = GreaseMonkeyJsObject(self)
afc2cda1a743 Implemented support for GreaseMonkey 4.0 for the web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6120
diff changeset
50
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 QTimer.singleShot(0, self.__load)
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
52
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 def showConfigurationDialog(self, parent=None):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 Public method to show the configuration dialog.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 @param parent reference to the parent widget (QWidget)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 """
3002
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2403
diff changeset
59 from .GreaseMonkeyConfiguration.GreaseMonkeyConfigurationDialog \
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2403
diff changeset
60 import GreaseMonkeyConfigurationDialog
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 self.__configDiaolg = GreaseMonkeyConfigurationDialog(self, parent)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 self.__configDiaolg.show()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63
4767
0bace7c5ebc9 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4766
diff changeset
64 def downloadScript(self, url):
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 Public method to download a GreaseMonkey script.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67
4767
0bace7c5ebc9 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4766
diff changeset
68 @param url URL to download script from
0bace7c5ebc9 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4766
diff changeset
69 @type QUrl
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 """
4886
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
71 QMetaObject.invokeMethod(
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
72 self, "doDownloadScript", Qt.QueuedConnection,
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
73 Q_ARG(QUrl, url))
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
74
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
75 @pyqtSlot(QUrl)
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
76 def doDownloadScript(self, url):
4917
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
77 """
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
78 Public slot to download a GreaseMonkey script.
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
79
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
80 Note: The download needed to be separated in the invoking part
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
81 (s.a.) and the one doing the real download because the invoking
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
82 part runs in a different thread (i.e. the web engine thread).
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
83
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
84 @param url URL to download script from
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
85 @type QUrl
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
86 """
2403
e3d7a861547c Continued implementing the delayed import.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
87 from .GreaseMonkeyDownloader import GreaseMonkeyDownloader
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
88 downloader = GreaseMonkeyDownloader(
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
89 url, self, GreaseMonkeyDownloader.DownloadMainScript)
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
90 downloader.finished.connect(
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
91 lambda f: self.__downloaderFinished(f, downloader))
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 self.__downloaders.append(downloader)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
94 def __downloaderFinished(self, fileName, downloader):
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 Private slot to handle the completion of a script download.
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
97
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
98 @param fileName name of the downloaded script
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
99 @type str
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
100 @param downloader reference to the downloader object
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
101 @type GreaseMonkeyDownloader
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 """
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
103 if downloader in self.__downloaders:
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
104 self.__downloaders.remove(downloader)
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
105
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
106 deleteScript = True
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
107 from .GreaseMonkeyScript import GreaseMonkeyScript
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
108 script = GreaseMonkeyScript(self, fileName)
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
109 if script.isValid():
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
110 if not self.containsScript(script.fullName()):
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
111 from .GreaseMonkeyAddScriptDialog import \
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
112 GreaseMonkeyAddScriptDialog
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
113 dlg = GreaseMonkeyAddScriptDialog(self, script)
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
114 deleteScript = dlg.exec_() != QDialog.Accepted
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
115 else:
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
116 E5MessageBox.information(
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
117 None,
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
118 QCoreApplication.translate(
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
119 "GreaseMonkeyManager",
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
120 "Install GreaseMonkey Script"),
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
121 QCoreApplication.translate(
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
122 "GreaseMonkeyManager",
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
123 """'{0}' is already installed.""").format(
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
124 script.fullName())
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
125 )
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
126
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
127 if deleteScript:
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
128 try:
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
129 os.remove(fileName)
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
130 except (IOError, OSError):
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
131 # ignore
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
132 pass
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 def scriptsDirectory(self):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 Public method to get the path of the scripts directory.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 @return path of the scripts directory (string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 """
3002
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2403
diff changeset
140 return os.path.join(
4763
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
141 Utilities.getConfigDir(), "web_browser", "greasemonkey")
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 def requireScriptsDirectory(self):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145 Public method to get the path of the scripts directory.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 @return path of the scripts directory (string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 return os.path.join(self.scriptsDirectory(), "requires")
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 def requireScripts(self, urlList):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153 Public method to get the sources of all required scripts.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 @param urlList list of URLs (list of string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 @return sources of all required scripts (string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
157 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 requiresDir = QDir(self.requireScriptsDirectory())
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 if not requiresDir.exists() or len(urlList) == 0:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 return ""
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 script = ""
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163
3002
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2403
diff changeset
164 settings = QSettings(
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2403
diff changeset
165 os.path.join(self.requireScriptsDirectory(), "requires.ini"),
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2403
diff changeset
166 QSettings.IniFormat)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 settings.beginGroup("Files")
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 for url in urlList:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169 if settings.contains(url):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 fileName = settings.value(url)
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
171 if not QFileInfo(fileName).isAbsolute():
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
172 fileName = os.path.join(self.requireScriptsDirectory(),
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
173 fileName)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
174 try:
1954
5072605ad4dd Fix for the GreaseMonkey code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1953
diff changeset
175 f = open(fileName, "r", encoding="utf-8")
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
176 source = f.read().strip()
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 f.close()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 except (IOError, OSError):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 source = ""
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
180 if source:
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
181 script += source + "\n"
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 return script
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 def saveConfiguration(self):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 Public method to save the configuration.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 """
4763
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
189 Preferences.setWebBrowser("GreaseMonkeyDisabledScripts",
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
190 self.__disabledScripts)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 def allScripts(self):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 Public method to get a list of all scripts.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 @return list of all scripts (list of GreaseMonkeyScript)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 """
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
198 return self.__scripts[:]
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 def containsScript(self, fullName):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 Public method to check, if the given script exists.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 @param fullName full name of the script (string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 @return flag indicating the existence (boolean)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 """
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
207 for script in self.__scripts:
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 if script.fullName() == fullName:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 return True
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
210
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 return False
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 def enableScript(self, script):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215 Public method to enable the given script.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 @param script script to be enabled (GreaseMonkeyScript)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219 script.setEnabled(True)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 fullName = script.fullName()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 if fullName in self.__disabledScripts:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 self.__disabledScripts.remove(fullName)
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
223
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
224 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
225 collection.insert(script.webScript())
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 def disableScript(self, script):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 Public method to disable the given script.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 @param script script to be disabled (GreaseMonkeyScript)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 script.setEnabled(False)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 fullName = script.fullName()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 if fullName not in self.__disabledScripts:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 self.__disabledScripts.append(fullName)
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
237
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
238 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
239 collection.remove(collection.findScript(fullName))
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 def addScript(self, script):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 Public method to add a script.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 @param script script to be added (GreaseMonkeyScript)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 @return flag indicating success (boolean)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 """
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
248 if not script or not script.isValid():
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 return False
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
251 self.__scripts.append(script)
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
252 script.scriptChanged.connect(lambda: self.__scriptChanged(script))
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
253
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
254 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
255 collection.insert(script.webScript())
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257 self.scriptsChanged.emit()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 return True
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
260 def removeScript(self, script, removeFile=True):
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 Public method to remove a script.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 @param script script to be removed (GreaseMonkeyScript)
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
265 @param removeFile flag indicating to remove the script file as well
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
266 (bool)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 @return flag indicating success (boolean)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 if not script:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 return False
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
272 try:
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
273 self.__scripts.remove(script)
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
274 except ValueError:
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
275 pass
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
277 fullName = script.fullName()
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
278 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
279 collection.remove(collection.findScript(fullName))
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
280
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
281 if fullName in self.__disabledScripts:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
282 self.__disabledScripts.remove(fullName)
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
283
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
284 if removeFile:
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
285 QFile.remove(script.fileName())
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
286 del script
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
287
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
288 self.scriptsChanged.emit()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289 return True
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 def canRunOnScheme(self, scheme):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
292 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
293 Public method to check, if scripts can be run on a scheme.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
294
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
295 @param scheme scheme to check (string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
296 @return flag indicating, that scripts can be run (boolean)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 return scheme in ["http", "https", "data", "ftp"]
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
299
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
300 def __load(self):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
301 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
302 Private slot to load the available scripts into the manager.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
303 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
304 scriptsDir = QDir(self.scriptsDirectory())
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
305 if not scriptsDir.exists():
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 scriptsDir.mkpath(self.scriptsDirectory())
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 if not scriptsDir.exists("requires"):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309 scriptsDir.mkdir("requires")
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310
3002
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2403
diff changeset
311 self.__disabledScripts = \
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
312 Preferences.getWebBrowser("GreaseMonkeyDisabledScripts")
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
313
2403
e3d7a861547c Continued implementing the delayed import.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
314 from .GreaseMonkeyScript import GreaseMonkeyScript
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315 for fileName in scriptsDir.entryList(["*.js"], QDir.Files):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
316 absolutePath = scriptsDir.absoluteFilePath(fileName)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 script = GreaseMonkeyScript(self, absolutePath)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
319 if not script.isValid():
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
320 del script
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
321 continue
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
322
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
323 self.__scripts.append(script)
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
324
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
325 if script.fullName() in self.__disabledScripts:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
326 script.setEnabled(False)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 else:
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
328 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
329 collection.insert(script.webScript())
6128
afc2cda1a743 Implemented support for GreaseMonkey 4.0 for the web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6120
diff changeset
330
afc2cda1a743 Implemented support for GreaseMonkey 4.0 for the web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6120
diff changeset
331 self.__jsObject.setSettingsFile(os.path.join(
afc2cda1a743 Implemented support for GreaseMonkey 4.0 for the web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6120
diff changeset
332 Utilities.getConfigDir(), "web_browser",
afc2cda1a743 Implemented support for GreaseMonkey 4.0 for the web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6120
diff changeset
333 "greasemonkey_values.ini"))
afc2cda1a743 Implemented support for GreaseMonkey 4.0 for the web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6120
diff changeset
334 ExternalJsObject.registerExtraObject("GreaseMonkey", self.__jsObject)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
335
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
336 def __scriptChanged(self, script):
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337 """
4763
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
338 Private slot handling a changed script.
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
339
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
340 @param script reference to the changed script
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
341 @type GreaseMonkeyScript
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
342 """
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
343 fullName = script.fullName()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
344 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
345 collection.remove(collection.findScript(fullName))
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
346 collection.insert(script.webScript())

eric ide

mercurial