Last active
October 11, 2020 13:20
-
-
Save arantius/3b493cb7ae6aad92d6c93881c14f1c4f to your computer and use it in GitHub Desktop.
Serves a user script, slowly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
PORT = 8000 | |
USER_JS = """// ==UserScript== | |
// @name Red Border | |
// @description A super simple user script with an unobtrusive way of being clear that it's running. | |
// @namespace test | |
// @include http* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
document.body.style.border = '3px dashed red'; | |
""" | |
for i in range(50): | |
USER_JS += '// %d ...\n' % i | |
class S(BaseHTTPRequestHandler): | |
def do_GET(self): | |
self.send_response(200) | |
if '.user.js' in self.path: | |
self.send_header('Content-Type', 'text/plain') | |
self.send_header('Content-Length', len(USER_JS)) | |
self.end_headers() | |
for line in USER_JS.splitlines(): | |
self.wfile.write(line + '\n') | |
time.sleep(0.25) | |
else: | |
self.send_header('Content-Type', 'text/html') | |
self.end_headers() | |
self.wfile.write( | |
'I can serve <a href="anything.user.js">a user script</a>, slowly.') | |
httpd = HTTPServer(('', PORT), S) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment