[PyQt] change values for javascript

Peter Irbizon peterirbizon at gmail.com
Fri Mar 28 21:39:24 GMT 2014


here is my python code for simple webbrowser I found on internet

#!/usr/bin/env python
import reimport sysimport PyQt4from PyQt4 import QtCore, QtGui
,QtWebKitfrom PyQt4 import uicfrom PyQt4.QtCore import *from
PyQt4.QtGui import *from PyQt4.QtWebKit import *from PyQt4.QtNetwork
import *


from PyQt4.QtCore import SIGNAL, QUrl, QString, Qt, QEventfrom
PyQt4.QtCore import QSize, QDateTime, QPointfrom PyQt4.QtGui import
QApplication, QImage, QPainterfrom PyQt4.QtGui import QCursor,
QMouseEvent, QKeyEventfrom PyQt4.QtNetwork import QNetworkCookie,
QNetworkAccessManagerfrom PyQt4.QtNetwork import QNetworkCookieJar,
QNetworkRequest, QNetworkProxyfrom PyQt4.QtWebKit import QWebPage,
QWebView
class WebPage(QWebPage):
    def __init__(self, user_agent):
        QWebPage.__init__(self)
        # set user agent
        self.user_agent = user_agent

    def userAgentForUrl(self, url):
        #~ return self.user_agent
        return QString(QByteArray(self.user_agent))

    def javaScriptAlert(self, frame, message):
        """Override default javascript alert popup
        """
        print 'Alert:', message

class IEWinApp:
 def __init__(self,url):
  self.url=url
  self.REGEXPS = \
  {
      'cookie':re.compile(r'(?P<cookie_name>\S*?)\s*=\s*(?P<cookie_value>\S*?);',
re.I | re.DOTALL)
  }
  cookie_input = "acookie=a; bcookie=b; ccookie=c;"
  #~ self.current = "http://www.useragentstring.com/"
  cookiebase = QNetworkCookie("","")
  self.cookies = cookiebase.parseCookies("")

  for cookiename,cookievalue in self.REGEXPS['cookie'].findall(cookie_input):
      cookie_tmp =
QNetworkCookie(QByteArray(cookiename),QByteArray(cookievalue))
      self.cookies.append(cookie_tmp)

  self.cookiejar = QNetworkCookieJar()
  #~ self.cookiejar.setCookiesFromUrl(self.cookies, QUrl(self.current))
  self.network_manager = QNetworkAccessManager()
  self.network_manager.setCookieJar(self.cookiejar)
  self.network_manager.sslErrors.connect(on_ssl_errors)
  #~ self.window = WebBrowser()
  self.page = IEWinPage()

  #~ agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)1"
  agent="default"
  self.page.setUserAgent(agent)
  self.page.setNetworkAccessManager(self.network_manager)

  self.frame = self.page.mainFrame()
  #~ self.window.webView.setPage(self.page)
  self.webView=QWebView()


  self.webView.setPage(self.page)
  self.webView.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled,True)
  #~ self.frame.load(QUrl(self.current))
  self.webView.load(QUrl(self.url))
  #~ self.window.show()
  self.webView.show()
def on_ssl_errors(reply, errors):
   url = unicode(reply.url().toString())
   reply.ignoreSslErrors()
   print "SSL certificate error ignored: %s" % url

class IEWinPage(QWebPage):
    def __init__(self):
        QWebPage.__init__(self)
        self.useragent = "Mozilla/4.0 (compatible; MSIE 7.0; Linux)"

    def setUserAgent(self, agent):
        if agent=="default":
          self.useragent = "Mozilla/5.0 (Linux; U; Linux; en-US;
rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28"
        else:
          self.useragent = agent

    def userAgentForUrl(self, url):
        return QString(QByteArray(self.useragent))



if __name__ == "__main__":
 app = QApplication(sys.argv)

 url= "http://facebook.com"

 win = IEWinApp(url)
 sys.exit( app.exec_() )

I am trying to make webbrowser and change values for Javascript - when
javascript print values

navigator.platform
navigator.appCodeName
navigator.appName
navigator.appVersion
navigator.productand screen values colorDepth, pixelDepthand getTimezoneOffset()

Problem is, javascript detects default system values for these properties
but I need to set it in python code so it will not print default system
values. Thank you!

I saw here, that I should execute this
http://stackoverflow.com/questions/2166540/how-can-i-fool-a-site-that-looks-at-the-javascript-object-navigator-to-see-thabefore
document loads but I don't know how. Please help
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140328/37ba4c3f/attachment-0001.html>


More information about the PyQt mailing list