eric6/WebBrowser/GreaseMonkey/GreaseMonkeyManager.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7923
91e843545d9a
child 8143
2c730d5fd177
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

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
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
3 # Copyright (c) 2012 - 2021 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
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import os
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
7268
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
12 from PyQt5.QtCore import (
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
13 pyqtSignal, pyqtSlot, Qt, QObject, QTimer, QFile, QFileInfo, QDir,
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
14 QSettings, QMetaObject, QUrl, Q_ARG, QCoreApplication
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
15 )
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
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 """
7268
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
59 from .GreaseMonkeyConfiguration import GreaseMonkeyConfigurationDialog
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
60 self.__configDiaolg = (
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
61 GreaseMonkeyConfigurationDialog.GreaseMonkeyConfigurationDialog(
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
62 self, parent)
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
63 )
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 self.__configDiaolg.show()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
4767
0bace7c5ebc9 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4766
diff changeset
66 def downloadScript(self, url):
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 Public method to download a GreaseMonkey script.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69
4767
0bace7c5ebc9 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4766
diff changeset
70 @param url URL to download script from
0bace7c5ebc9 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4766
diff changeset
71 @type QUrl
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 """
4886
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
73 QMetaObject.invokeMethod(
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
74 self, "doDownloadScript", Qt.QueuedConnection,
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
75 Q_ARG(QUrl, url))
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
76
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
77 @pyqtSlot(QUrl)
b56735d36159 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4767
diff changeset
78 def doDownloadScript(self, url):
4917
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 Public slot to download a GreaseMonkey script.
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
81
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
82 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
83 (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
84 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
85
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
86 @param url URL to download script from
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
87 @type QUrl
682750cc7bd5 Corrected some code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4913
diff changeset
88 """
2403
e3d7a861547c Continued implementing the delayed import.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
89 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
90 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
91 url, self, GreaseMonkeyDownloader.DownloadMainScript)
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
92 downloader.finished.connect(
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
93 lambda f: self.__downloaderFinished(f, downloader))
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 self.__downloaders.append(downloader)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
96 def __downloaderFinished(self, fileName, downloader):
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 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
99
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
100 @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
101 @type str
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
102 @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
103 @type GreaseMonkeyDownloader
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 """
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
105 if downloader in self.__downloaders:
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
106 self.__downloaders.remove(downloader)
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
107
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
108 deleteScript = True
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
109 from .GreaseMonkeyScript import GreaseMonkeyScript
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
110 script = GreaseMonkeyScript(self, fileName)
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
111 if script.isValid():
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
112 if not self.containsScript(script.fullName()):
7268
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
113 from .GreaseMonkeyAddScriptDialog import (
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
114 GreaseMonkeyAddScriptDialog
7268
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
115 )
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
116 dlg = GreaseMonkeyAddScriptDialog(self, script)
7759
51aa6c6b66f7 Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7360
diff changeset
117 deleteScript = dlg.exec() != QDialog.Accepted
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
118 else:
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
119 E5MessageBox.information(
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
120 None,
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 "Install GreaseMonkey Script"),
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
124 QCoreApplication.translate(
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
125 "GreaseMonkeyManager",
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
126 """'{0}' is already installed.""").format(
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
127 script.fullName())
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
128 )
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
129
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
130 if deleteScript:
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
131 try:
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
132 os.remove(fileName)
7836
2f0d208b8137 Changed code to not use the OSError aliases (IOError, EnvironmentError, socket.error and select.error) anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
133 except OSError:
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
134 # ignore
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
135 pass
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 def scriptsDirectory(self):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 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
140
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 @return path of the scripts directory (string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 """
3002
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2403
diff changeset
143 return os.path.join(
4763
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
144 Utilities.getConfigDir(), "web_browser", "greasemonkey")
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 def requireScriptsDirectory(self):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 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
149
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 @return path of the scripts directory (string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 return os.path.join(self.scriptsDirectory(), "requires")
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
154 def requireScripts(self, urlList):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
155 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
156 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
157
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
158 @param urlList list of URLs (list of string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159 @return sources of all required scripts (string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 requiresDir = QDir(self.requireScriptsDirectory())
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 if not requiresDir.exists() or len(urlList) == 0:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163 return ""
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
164
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165 script = ""
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166
3002
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2403
diff changeset
167 settings = QSettings(
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2403
diff changeset
168 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
169 QSettings.IniFormat)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 settings.beginGroup("Files")
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 for url in urlList:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 if settings.contains(url):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 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
174 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
175 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
176 fileName)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 try:
7785
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
178 with open(fileName, "r", encoding="utf-8") as f:
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
179 source = f.read().strip()
7836
2f0d208b8137 Changed code to not use the OSError aliases (IOError, EnvironmentError, socket.error and select.error) anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
180 except OSError:
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 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
182 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
183 script += source + "\n"
1953
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 return script
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 def saveConfiguration(self):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 Public method to save the configuration.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 """
4763
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
191 Preferences.setWebBrowser("GreaseMonkeyDisabledScripts",
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
192 self.__disabledScripts)
1953
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 def allScripts(self):
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 Public method to get a list of all scripts.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 @return list of all scripts (list of GreaseMonkeyScript)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 """
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
200 return self.__scripts[:]
1953
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 def containsScript(self, fullName):
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 Public method to check, if the given script exists.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 @param fullName full name of the script (string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 @return flag indicating the existence (boolean)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 """
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
209 for script in self.__scripts:
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 if script.fullName() == fullName:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 return True
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
212
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 return False
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 def enableScript(self, 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 Public method to enable the given script.
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 @param script script to be enabled (GreaseMonkeyScript)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 script.setEnabled(True)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 fullName = script.fullName()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 if fullName in self.__disabledScripts:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 self.__disabledScripts.remove(fullName)
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
225
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
226 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
227 collection.insert(script.webScript())
1953
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 def disableScript(self, 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 Public method to disable the given script.
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 @param script script to be disabled (GreaseMonkeyScript)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235 script.setEnabled(False)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 fullName = script.fullName()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 if fullName not in self.__disabledScripts:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 self.__disabledScripts.append(fullName)
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
239
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
240 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
241 collection.remove(collection.findScript(fullName))
1953
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 def addScript(self, 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 Public method to add a script.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 @param script script to be added (GreaseMonkeyScript)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248 @return flag indicating success (boolean)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 """
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
250 if not script or not script.isValid():
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 return False
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
253 self.__scripts.append(script)
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
254 script.scriptChanged.connect(lambda: self.__scriptChanged(script))
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
255
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
256 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
257 collection.insert(script.webScript())
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 self.scriptsChanged.emit()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
260 return True
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
262 def removeScript(self, script, removeFile=True):
1953
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 Public method to remove a script.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 @param script script to be removed (GreaseMonkeyScript)
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
267 @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
268 (bool)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 @return flag indicating success (boolean)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 if not script:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 return False
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
274 try:
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
275 self.__scripts.remove(script)
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
276 except ValueError:
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
277 pass
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
278
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
279 fullName = script.fullName()
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
280 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
281 collection.remove(collection.findScript(fullName))
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
282
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
283 if fullName in self.__disabledScripts:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
284 self.__disabledScripts.remove(fullName)
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
285
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
286 if removeFile:
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
287 QFile.remove(script.fileName())
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
288 del script
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
289
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
290 self.scriptsChanged.emit()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
291 return True
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 def canRunOnScheme(self, 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 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
296
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
297 @param scheme scheme to check (string)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
298 @return flag indicating, that scripts can be run (boolean)
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 return scheme in ["http", "https", "data", "ftp"]
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 def __load(self):
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 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
305 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
306 scriptsDir = QDir(self.scriptsDirectory())
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
307 if not scriptsDir.exists():
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
308 scriptsDir.mkpath(self.scriptsDirectory())
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
309
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
310 if not scriptsDir.exists("requires"):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
311 scriptsDir.mkdir("requires")
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
312
7268
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
313 self.__disabledScripts = Preferences.getWebBrowser(
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
314 "GreaseMonkeyDisabledScripts")
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
315
2403
e3d7a861547c Continued implementing the delayed import.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
316 from .GreaseMonkeyScript import GreaseMonkeyScript
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
317 for fileName in scriptsDir.entryList(["*.js"], QDir.Files):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
318 absolutePath = scriptsDir.absoluteFilePath(fileName)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
319 script = GreaseMonkeyScript(self, absolutePath)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
320
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
321 if not script.isValid():
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
322 del script
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
323 continue
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
324
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
325 self.__scripts.append(script)
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
326
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
327 if script.fullName() in self.__disabledScripts:
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 script.setEnabled(False)
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
329 else:
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
330 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
331 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
332
afc2cda1a743 Implemented support for GreaseMonkey 4.0 for the web browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6120
diff changeset
333 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
334 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
335 "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
336 ExternalJsObject.registerExtraObject("GreaseMonkey", self.__jsObject)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
337
6120
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
338 def __scriptChanged(self, script):
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
339 """
4763
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
340 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
341
4c60a21ce6dd Continued removing the use of QObject.sender().
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
342 @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
343 @type GreaseMonkeyScript
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
344 """
4766
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
345 fullName = script.fullName()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
346 collection = WebBrowserWindow.webProfile().scripts()
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
347 collection.remove(collection.findScript(fullName))
5f8d08aa2217 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
348 collection.insert(script.webScript())

eric ide

mercurial