WebBrowser/GreaseMonkey/GreaseMonkeyScript.py

changeset 6145
dfe864318196
parent 6120
4c60a21ce6dd
child 6645
ad476851d7e0
--- a/WebBrowser/GreaseMonkey/GreaseMonkeyScript.py	Tue Feb 13 19:51:20 2018 +0100
+++ b/WebBrowser/GreaseMonkey/GreaseMonkeyScript.py	Tue Feb 13 19:52:13 2018 +0100
@@ -11,6 +11,8 @@
 
 from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QUrl, QRegExp, \
     QByteArray, QCryptographicHash
+from PyQt5.QtGui import QIcon, QPixmap, QImage
+from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
 from PyQt5.QtWebEngineWidgets import QWebEngineScript
 
 from .GreaseMonkeyJavaScript import bootstrap_js, values_js
@@ -18,6 +20,7 @@
 
 from ..Tools.DelayedFileWatcher import DelayedFileWatcher
 from ..WebBrowserPage import WebBrowserPage
+from ..WebBrowserWindow import WebBrowserWindow
 
 from Globals import qVersionTuple
 
@@ -58,6 +61,8 @@
         self.__exclude = []
         self.__require = []
         
+        self.__icon = QIcon()
+        self.__iconUrl = QUrl()
         self.__downloadUrl = QUrl()
         self.__updateUrl = QUrl()
         self.__startAt = GreaseMonkeyScript.DocumentEnd
@@ -71,6 +76,7 @@
         self.__updating = False
         
         self.__downloaders = []
+        self.__iconReplies = []
         
         self.__parseScript()
         
@@ -125,6 +131,23 @@
         """
         return self.__version
     
+    def icon(self):
+        """
+        Public method to get the icon of the script.
+        
+        @return script icon
+        @rtype QIcon
+        """
+        return self.__icon
+    
+    def iconUrl(self):
+        """
+        Public method to get the icon URL of the script.
+        
+        @return icon URL of the script (QUrl)
+        """
+        return QUrl(self.__iconUrl)
+    
     def downloadUrl(self):
         """
         Public method to get the download URL of the script.
@@ -240,6 +263,8 @@
         self.__exclude = []
         self.__require = []
         
+        self.__icon = QIcon()
+        self.__iconUrl = QUrl()
         self.__downloadUrl = QUrl()
         self.__updateUrl = QUrl()
         self.__startAt = GreaseMonkeyScript.DocumentEnd
@@ -277,15 +302,14 @@
             
             line = line[3:].replace("\t", " ")
             index = line.find(" ")
-            if index < 0:
-                continue
             
             key = line[:index].strip()
-            value = line[index + 1:].strip()
+            if index > 0:
+                value = line[index + 1:].strip()
+            else:
+                value = ""
             
-            # Ignored values: @resource, @unwrap
-            
-            if not key or not value:
+            if not key:
                 continue
             
             if key == "@name":
@@ -322,6 +346,14 @@
             
             elif key == "@updateURL" and self.__updateUrl.isEmpty():
                 self.__updateUrl = QUrl(value)
+            
+            elif key == "@icon":
+                self.__iconUrl = QUrl(value)
+            
+            elif key == "@noframes":
+                self.__noFrames = True
+        
+        self.__iconUrl = self.__downloadUrl.resolved(self.__iconUrl)
         
         if not self.__include:
             self.__include.append("*")
@@ -363,6 +395,9 @@
                 fileData
             )
         self.__valid = True
+        
+        self.__downloadIcon()
+        self.__downloadRequires()
     
     def webScript(self):
         """
@@ -514,3 +549,27 @@
         """
         if downloader in self.__downloaders:
             self.__downloaders.remove(downloader)
+    
+    def __downloadIcon(self):
+        """
+        Private slot to download the script icon.
+        """
+        if self.__iconUrl.isValid():
+            request = QNetworkRequest(self.__iconUrl)
+            reply = WebBrowserWindow.networkManager().get(request)
+            reply.finished.connect(lambda: self.__iconDownloaded(reply))
+            self.__iconReplies.append(reply)
+    
+    def __iconDownloaded(self, reply):
+        """
+        Private slot to handle a finished download of a script icon.
+        
+        @param reply reference to the network reply
+        @type QNetworkReply
+        """
+        if reply in self.__iconReplies:
+            self.__iconReplies.remove(reply)
+        
+        reply.deleteLater()
+        if reply.error() == QNetworkReply.NoError:
+            self.__icon = QPixmap.fromImage(QImage.fromData(reply.readAll()))

eric ide

mercurial