def createRequest(self, op, request, outgoingData=None): """ Public method to create a request. @param op the operation to be performed (QNetworkAccessManager.Operation) @param request reference to the request object (QNetworkRequest) @param outgoingData reference to an IODevice containing data to be sent (QIODevice) @return reference to the created reply object (QNetworkReply) """ if op != QNetworkAccessManager.GetOperation: return None url = request.url() if url.path() != "subscribe": return None if qVersion() >= "5.0.0": from PyQt5.QtCore import QUrlQuery, QUrl title = QUrl.fromPercentEncoding( QByteArray(QUrlQuery(url).queryItemValue("title").encode())) else: from PyQt5.QtCore import QUrl title = QUrl.fromPercentEncoding( url.encodedQueryItemValue(b"title")) if not title: return None res = E5MessageBox.yesNo( None, self.tr("Subscribe?"), self.tr( """<p>Subscribe to this AdBlock subscription?</p>""" """<p>{0}</p>""").format(title)) if res: from .AdBlockSubscription import AdBlockSubscription import Helpviewer.HelpWindow dlg = Helpviewer.HelpWindow.HelpWindow.adBlockManager()\ .showDialog() subscription = AdBlockSubscription( url, False, Helpviewer.HelpWindow.HelpWindow.adBlockManager()) Helpviewer.HelpWindow.HelpWindow.adBlockManager()\ .addSubscription(subscription) dlg.addSubscription(subscription, False) dlg.setFocus() dlg.raise_() return EmptyNetworkReply(self.parent())
def handle_unsupported_content(self, reply): """Handle requests to open non-web content Called basically when the reply from the request is not HTML or something else renderable by qwebview. It checks the configured content-handlers for a matching MIME type, and opens the file or displays an error per the configuration. """ self.reply = reply self.content_type = self.reply.header( QNetworkRequest.ContentTypeHeader).toString() self.content_filename = re.match( '.*;\s*filename=(.*);', self.reply.rawHeader('Content-Disposition')) self.content_filename = QUrl.fromPercentEncoding( (self.content_filename and self.content_filename.group(1)) or '') content_url = self.reply.url() debug("Loading url {} of type {}".format(content_url.toString(), self.content_type)) if not self.config.get("content_handlers").get(str(self.content_type)): self.setHtml( UNKNOWN_CONTENT_TYPE.format(mime_type=self.content_type, file_name=self.content_filename, url=content_url.toString())) else: if str(self.url().toString()) in ('', 'about:blank'): self.setHtml( DOWNLOADING_MESSAGE.format(filename=self.content_filename, mime_type=self.content_type, url=content_url.toString())) else: # print(self.url()) self.load(self.url()) self.reply.finished.connect(self.display_downloaded_content)
def addSubscriptionFromUrl(self, url): """ Public method to ad an AdBlock subscription given the abp URL. @param url URL to subscribe an AdBlock subscription @type QUrl @return flag indicating success @rtype bool """ if url.path() != "subscribe": return False title = QUrl.fromPercentEncoding( QByteArray(QUrlQuery(url).queryItemValue("title").encode())) if not title: return False res = E5MessageBox.yesNo( None, self.tr("Subscribe?"), self.tr("""<p>Subscribe to this AdBlock subscription?</p>""" """<p>{0}</p>""").format(title)) if res: from .AdBlockSubscription import AdBlockSubscription from WebBrowser.WebBrowserWindow import WebBrowserWindow dlg = WebBrowserWindow.adBlockManager().showDialog() subscription = AdBlockSubscription( url, False, WebBrowserWindow.adBlockManager()) WebBrowserWindow.adBlockManager().addSubscription(subscription) dlg.addSubscription(subscription, False) dlg.setFocus() dlg.raise_() return res
def __parseUrl(self, url): """ Private method to parse the AdBlock URL for the subscription. @param url AdBlock URL for the subscription (QUrl) """ if url.scheme() != "abp": return if url.path() != "subscribe": return if qVersion() >= "5.0.0": from PyQt5.QtCore import QUrlQuery urlQuery = QUrlQuery(url) self.__title = urlQuery.queryItemValue("title") self.__enabled = urlQuery.queryItemValue("enabled") != "false" self.__location = QByteArray(urlQuery.queryItemValue("location")) # Check for required subscription self.__requiresLocation = urlQuery.queryItemValue( "requiresLocation") self.__requiresTitle = urlQuery.queryItemValue("requiresTitle") if self.__requiresLocation and self.__requiresTitle: import Helpviewer.HelpWindow Helpviewer.HelpWindow.HelpWindow.adBlockManager()\ .loadRequiredSubscription(self.__requiresLocation, self.__requiresTitle) lastUpdateString = urlQuery.queryItemValue("lastUpdate") self.__lastUpdate = QDateTime.fromString(lastUpdateString, Qt.ISODate) else: self.__title = \ QUrl.fromPercentEncoding(url.encodedQueryItemValue("title")) self.__enabled = QUrl.fromPercentEncoding( url.encodedQueryItemValue("enabled")) != "false" self.__location = QByteArray( QUrl.fromPercentEncoding( url.encodedQueryItemValue("location"))) # Check for required subscription self.__requiresLocation = QUrl.fromPercentEncoding( url.encodedQueryItemValue("requiresLocation")) self.__requiresTitle = QUrl.fromPercentEncoding( url.encodedQueryItemValue("requiresTitle")) if self.__requiresLocation and self.__requiresTitle: import Helpviewer.HelpWindow Helpviewer.HelpWindow.HelpWindow.adBlockManager()\ .loadRequiredSubscription(self.__requiresLocation, self.__requiresTitle) lastUpdateByteArray = url.encodedQueryItemValue("lastUpdate") lastUpdateString = QUrl.fromPercentEncoding(lastUpdateByteArray) self.__lastUpdate = QDateTime.fromString(lastUpdateString, Qt.ISODate) self.__loadRules()
def __parseUrl(self, url): """ Private method to parse the AdBlock URL for the subscription. @param url AdBlock URL for the subscription @type QUrl """ if url.scheme() != "abp": return if url.path() != "subscribe": return urlQuery = QUrlQuery(url) self.__title = QUrl.fromPercentEncoding( QByteArray(urlQuery.queryItemValue("title").encode())) self.__enabled = urlQuery.queryItemValue("enabled") != "false" self.__location = QByteArray(QUrl.fromPercentEncoding( QByteArray(urlQuery.queryItemValue("location").encode())) .encode("utf-8")) # Check for required subscription self.__requiresLocation = QUrl.fromPercentEncoding( QByteArray(urlQuery.queryItemValue( "requiresLocation").encode())) self.__requiresTitle = QUrl.fromPercentEncoding( QByteArray(urlQuery.queryItemValue("requiresTitle").encode())) if self.__requiresLocation and self.__requiresTitle: from WebBrowser.WebBrowserWindow import WebBrowserWindow WebBrowserWindow.adBlockManager().loadRequiredSubscription( self.__requiresLocation, self.__requiresTitle) lastUpdateString = urlQuery.queryItemValue("lastUpdate") self.__lastUpdate = QDateTime.fromString(lastUpdateString, Qt.ISODate) self.__loadRules()
def __parseUrl(self, url): """ Private method to parse the AdBlock URL for the subscription. @param url AdBlock URL for the subscription (QUrl) """ if url.scheme() != "abp": return if url.path() != "subscribe": return if qVersion() >= "5.0.0": from PyQt5.QtCore import QUrlQuery urlQuery = QUrlQuery(url) self.__title = urlQuery.queryItemValue("title") self.__enabled = urlQuery.queryItemValue("enabled") != "false" self.__location = QByteArray(urlQuery.queryItemValue("location")) # Check for required subscription self.__requiresLocation = urlQuery.queryItemValue( "requiresLocation") self.__requiresTitle = urlQuery.queryItemValue("requiresTitle") if self.__requiresLocation and self.__requiresTitle: import Helpviewer.HelpWindow Helpviewer.HelpWindow.HelpWindow.adBlockManager()\ .loadRequiredSubscription(self.__requiresLocation, self.__requiresTitle) lastUpdateString = urlQuery.queryItemValue("lastUpdate") self.__lastUpdate = QDateTime.fromString(lastUpdateString, Qt.ISODate) else: self.__title = \ QUrl.fromPercentEncoding(url.encodedQueryItemValue("title")) self.__enabled = QUrl.fromPercentEncoding( url.encodedQueryItemValue("enabled")) != "false" self.__location = QByteArray(QUrl.fromPercentEncoding( url.encodedQueryItemValue("location"))) # Check for required subscription self.__requiresLocation = QUrl.fromPercentEncoding( url.encodedQueryItemValue("requiresLocation")) self.__requiresTitle = QUrl.fromPercentEncoding( url.encodedQueryItemValue("requiresTitle")) if self.__requiresLocation and self.__requiresTitle: import Helpviewer.HelpWindow Helpviewer.HelpWindow.HelpWindow.adBlockManager()\ .loadRequiredSubscription(self.__requiresLocation, self.__requiresTitle) lastUpdateByteArray = url.encodedQueryItemValue("lastUpdate") lastUpdateString = QUrl.fromPercentEncoding(lastUpdateByteArray) self.__lastUpdate = QDateTime.fromString(lastUpdateString, Qt.ISODate) self.__loadRules()
def handle_unsupported_content(self, reply): """Handle requests to open non-web content Called basically when the reply from the request is not HTML or something else renderable by qwebview. It checks the configured content-handlers for a matching MIME type, and opens the file or displays an error per the configuration. """ self.reply = reply self.content_type = self.reply.header( QNetworkRequest.ContentTypeHeader ) self.content_filename = re.match( '.*;\s*filename=(.*);', bytes(self.reply.rawHeader(b'Content-Disposition')).decode('UTF-8') ) self.content_filename = QUrl.fromPercentEncoding( ( self.content_filename.group(1).encode('UTF-8') if self.content_filename else b'' ) ) content_url = self.reply.url() debug( "Loading url {} of type {}".format( content_url.toString(), self.content_type )) if not self.config.get("content_handlers").get(str(self.content_type)): self.setHtml(UNKNOWN_CONTENT_TYPE.format( mime_type=self.content_type, file_name=self.content_filename, url=content_url.toString())) else: if self.reply.isFinished: self.display_downloaded_content() else: self.reply.finished.connect(self.display_downloaded_content) if str(self.url().toString()) in ('', 'about:blank'): self.setHtml(DOWNLOADING_MESSAGE.format( filename=self.content_filename, mime_type=self.content_type, url=content_url.toString())) else: # print(self.url()) self.load(self.url())
def __findForm(self, webPage, data, boundary=None): """ Private method to find the form used for logging in. @param webPage reference to the web page (QWebPage) @param data data to be sent (QByteArray) @keyparam boundary boundary string (QByteArray) for multipart encoded data, None for urlencoded data @return parsed form (LoginForm) """ from .LoginForm import LoginForm form = LoginForm() if boundary is not None: args = self.__extractMultipartQueryItems(data, boundary) else: if qVersion() >= "5.0.0": from PyQt5.QtCore import QUrlQuery argsUrl = QUrl.fromEncoded( QByteArray(b"foo://bar.com/?" + QUrl.fromPercentEncoding( data.replace(b"+", b"%20")).encode("utf-8"))) encodedArgs = QUrlQuery(argsUrl).queryItems() else: argsUrl = QUrl.fromEncoded( QByteArray(b"foo://bar.com/?" + data.replace(b"+", b"%20")) ) encodedArgs = argsUrl.queryItems() args = set() for arg in encodedArgs: key = arg[0] value = arg[1] args.add((key, value)) # extract the forms from Helpviewer.JavaScriptResources import parseForms_js lst = webPage.mainFrame().evaluateJavaScript(parseForms_js) for map in lst: formHasPasswords = False formName = map["name"] formIndex = map["index"] if isinstance(formIndex, float) and formIndex.is_integer(): formIndex = int(formIndex) elements = map["elements"] formElements = set() formElementTypes = {} deadElements = set() for elementMap in elements: try: name = elementMap["name"] value = elementMap["value"] type_ = elementMap["type"] except KeyError: continue if type_ == "password": formHasPasswords = True t = (name, value) try: if elementMap["autocomplete"] == "off": deadElements.add(t) except KeyError: pass if name: formElements.add(t) formElementTypes[name] = type_ if formElements.intersection(args) == args: form.hasAPassword = formHasPasswords if not formName: form.name = formIndex else: form.name = formName args.difference_update(deadElements) for elt in deadElements: if elt[0] in formElementTypes: del formElementTypes[elt[0]] form.elements = list(args) form.elementTypes = formElementTypes break return form
def qurlPercentDecode(qurl): return QUrl(QUrl.fromPercentEncoding(qurl.toEncoded())).toString()