Skip to content

Instantly share code, notes, and snippets.

@collinalexbell
Last active October 19, 2017 12:41
Show Gist options
  • Save collinalexbell/567e6d40388047b7d79d7cb483502354 to your computer and use it in GitHub Desktop.
Save collinalexbell/567e6d40388047b7d79d7cb483502354 to your computer and use it in GitHub Desktop.
A flask server used to inject javascript into a window to test using PhantomJS
import threading
from yattag import Doc
from flask import Flask
js_scripts = []
app = (Flask(__name__))
thread = None
def start(host='0.0.0.0', port = 6969):
"""Gets app listening on host:port. Call inject before starting the
Args:
host (str):
port (int):
Returns:
None
"""
global thread
thread = threading.Thread(target=app.run,
kwargs={'host':host,
'port':port})
thread.start()
def inject(js_script):
"""Add js_scripts to render.
Args:
js_script (str):The JS content you would like to inject (will wrap in <script> tag)
Returns:
None
"""
global js_scripts
js_scripts.append(js_script)
print(js_scripts)
@app.route('/')
def home():
doc, tag, text = Doc().tagtext()
with tag('html'):
with tag('body'):
for script in js_scripts:
print(script)
with tag('script'):
text(script)
print(doc.getvalue())
return doc.getvalue()
if __name__ == '__main__':
start()
inject("alert('1x1 let me see ya\\n2x2 for the sequel\\n3x3 for my people')")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment