Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Serves a user script, slowly
#!/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
You can’t perform that action at this time.