mozchecker.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 gi
  19. gi.require_version('Gtk', '3.0')
  20. gi.require_version('Gdk', '3.0')
  21. from gi.repository import Gtk as gtk
  22. from gi.repository import Gdk as gdk
  23. gdk.threads_init()
  24. import sys
  25. from gi.repository import GObject as gobject
  26. import subprocess
  27. from threading import Thread
  28. try:
  29. from gtkmozembed import MozEmbed
  30. except:
  31. MozEmbed = None
  32. import webbrowser
  33. class CheckerThread(Thread):
  34. def __init__(self, parent, url):
  35. Thread.__init__(self)
  36. self.daemon = True
  37. self._armed = True
  38. self._url = url
  39. self._parent = parent
  40. def shutdown(self):
  41. if self.result:
  42. self._armed = False
  43. self.result.terminate()
  44. def run(self):
  45. self.result = subprocess.Popen([sys.executable, __file__, self._url],
  46. stderr=subprocess.PIPE)
  47. self.result.wait()
  48. if self._armed:
  49. self._parent.on_net_stop()
  50. self.result = None
  51. class MozChecker(object):
  52. def __init__(self, parent):
  53. self._busy = False
  54. self._urlqueue = []
  55. self._parent = parent
  56. self._armed = True
  57. if MozEmbed:
  58. pass
  59. else:
  60. self.open = self.open_webbrowser
  61. def remaining(self):
  62. return len(self._urlqueue)
  63. def init_mozembed(self):
  64. self.moz = MozEmbed()
  65. self.moz.connect('net-stop', self.on_net_stop)
  66. self.moz.connect('net-state', self.on_net_state)
  67. self.moz.connect('new-window', self.on_new_window)
  68. self.add(self.moz)
  69. self.moz.show()
  70. def on_new_window(self, widget, retval, chromemask):
  71. print("new window")
  72. print(widget, retval, chromemask)
  73. return False
  74. def open_webbrowser(self, url):
  75. webbrowser.open(url, 2, False)
  76. def open_job(self, url):
  77. if self._parent:
  78. self._parent.start_token_check(url)
  79. self._busy = CheckerThread(self, url)
  80. self._busy.start()
  81. def shutdown(self):
  82. if self._busy:
  83. self._armed = False
  84. self._busy.shutdown()
  85. self._busy.join()
  86. def open(self, url):
  87. if not self._busy:
  88. self.open_job(url)
  89. else:
  90. self._urlqueue.append(url)
  91. def on_js_status(self, widget):
  92. widget.get_js_status()
  93. def on_net_state(self, widget, flags, status):
  94. print("net_state", widget, flags, status)
  95. def on_net_stop(self, widget=None):
  96. gdk.threads_enter()
  97. gobject.timeout_add(0, self.process_next)
  98. gdk.threads_leave()
  99. def process_next(self):
  100. if self._urlqueue and self._armed:
  101. next_url = self._urlqueue.pop(0)
  102. self.open_job(next_url)
  103. else:
  104. self._busy = False
  105. if __name__ == '__main__':
  106. win = gtk.Window()
  107. def finished(widget):
  108. gtk.main_quit()
  109. def alertkill():
  110. for a in gtk.window_list_toplevels():
  111. if a.get_title() and (a.get_title() == 'Alert' or 'says' in a.get_title() or 'Warning' in a.get_title()):
  112. print(a.get_children())
  113. a.hide()
  114. a.destroy()
  115. gtk.main_quit()
  116. gobject.timeout_add(100, alertkill)
  117. def bailout():
  118. gtk.main_quit()
  119. sys.exit()
  120. def unmap(widget):
  121. widget.hide()
  122. def new_window(widget, retval, mask):
  123. print("new window!!")
  124. gobject.timeout_add(30000, bailout)
  125. gobject.timeout_add(100, alertkill)
  126. win = gtk.Window()
  127. win.set_property('skip-taskbar-hint', True)
  128. win.set_property('skip-pager-hint', True)
  129. win.set_keep_below(True)
  130. win.connect('map', unmap)
  131. moz = MozEmbed()
  132. moz.load_url(sys.argv[1])
  133. moz.connect('net-stop', finished)
  134. moz.connect('new-window', new_window)
  135. win.set_title(sys.argv[1])
  136. win.add(moz)
  137. win.show_all()
  138. gtk.main()