mozchecker.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. # vim: set expandtab tabstop=4 shiftwidth=4:
  4. """
  5. This file is part of the XSSer project, https://xsser.03c8.net
  6. Copyright (c) 2010/2019 | psy <epsylon@riseup.net>
  7. xsser is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation version 3 of the License.
  10. xsser is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. details.
  14. You should have received a copy of the GNU General Public License along
  15. with xsser; if not, write to the Free Software Foundation, Inc., 51
  16. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. """
  18. import gtk
  19. import sys
  20. import gobject
  21. import subprocess
  22. from threading import Thread
  23. try:
  24. from gtkmozembed import MozEmbed
  25. except:
  26. MozEmbed = None
  27. import webbrowser
  28. class CheckerThread(Thread):
  29. def __init__(self, parent, url):
  30. Thread.__init__(self)
  31. self.daemon = True
  32. self._armed = True
  33. self._url = url
  34. self._parent = parent
  35. def shutdown(self):
  36. if self.result:
  37. self._armed = False
  38. self.result.terminate()
  39. def run(self):
  40. self.result = subprocess.Popen([sys.executable, __file__, self._url],
  41. stderr=subprocess.PIPE)
  42. self.result.wait()
  43. if self._armed:
  44. self._parent.on_net_stop()
  45. self.result = None
  46. class MozChecker(object):
  47. def __init__(self, parent):
  48. self._busy = False
  49. self._urlqueue = []
  50. self._parent = parent
  51. self._armed = True
  52. if MozEmbed:
  53. pass
  54. else:
  55. self.open = self.open_webbrowser
  56. def remaining(self):
  57. return len(self._urlqueue)
  58. def init_mozembed(self):
  59. self.moz = MozEmbed()
  60. self.moz.connect('net-stop', self.on_net_stop)
  61. self.moz.connect('net-state', self.on_net_state)
  62. self.moz.connect('new-window', self.on_new_window)
  63. self.add(self.moz)
  64. self.moz.show()
  65. def on_new_window(self, widget, retval, chromemask):
  66. print("new window")
  67. print(widget, retval, chromemask)
  68. return False
  69. def open_webbrowser(self, url):
  70. webbrowser.open(url, 2, False)
  71. def open_job(self, url):
  72. if self._parent:
  73. self._parent.start_token_check(url)
  74. self._busy = CheckerThread(self, url)
  75. self._busy.start()
  76. def shutdown(self):
  77. if self._busy:
  78. self._armed = False
  79. self._busy.shutdown()
  80. self._busy.join()
  81. def open(self, url):
  82. if not self._busy:
  83. self.open_job(url)
  84. else:
  85. self._urlqueue.append(url)
  86. def on_js_status(self, widget):
  87. widget.get_js_status()
  88. def on_net_state(self, widget, flags, status):
  89. print("net_state", widget, flags, status)
  90. def on_net_stop(self, widget=None):
  91. gtk.gdk.threads_enter()
  92. gobject.timeout_add(0, self.process_next)
  93. gtk.gdk.threads_leave()
  94. def process_next(self):
  95. if self._urlqueue and self._armed:
  96. next_url = self._urlqueue.pop(0)
  97. self.open_job(next_url)
  98. else:
  99. self._busy = False
  100. if __name__ == '__main__':
  101. win = gtk.Window()
  102. def finished(widget):
  103. gtk.main_quit()
  104. def alertkill():
  105. for a in gtk.window_list_toplevels():
  106. if a.get_title() and (a.get_title() == 'Alert' or 'says' in a.get_title() or 'Warning' in a.get_title()):
  107. print(a.get_children())
  108. a.hide()
  109. a.destroy()
  110. gtk.main_quit()
  111. gobject.timeout_add(100, alertkill)
  112. def bailout():
  113. gtk.main_quit()
  114. sys.exit()
  115. def unmap(widget):
  116. widget.hide()
  117. def new_window(widget, retval, mask):
  118. print("new window!!")
  119. gobject.timeout_add(30000, bailout)
  120. gobject.timeout_add(100, alertkill)
  121. win = gtk.Window()
  122. win.set_property('skip-taskbar-hint', True)
  123. win.set_property('skip-pager-hint', True)
  124. win.set_keep_below(True)
  125. win.connect('map', unmap)
  126. moz = MozEmbed()
  127. moz.load_url(sys.argv[1])
  128. moz.connect('net-stop', finished)
  129. moz.connect('new-window', new_window)
  130. win.set_title(sys.argv[1])
  131. win.add(moz)
  132. win.show_all()
  133. gtk.main()