Recently Written · git

subread-extension

Show your own .srt over a YouTube video, in time with it. Chrome and Firefox.

git clone https://github.com/equwal/subread-extension

Log | Files | Refs


tests/mock/server.py (2285 bytes)

1 """A stand-in for a YouTube watch page, to try the content script by hand:
2 
3     python tests/mock/server.py        then open http://localhost:8432/watch?v=mock
4 
5 It has what the script looks for (#movie_player with a <video>, the /watch?v=
6 address) and a silent video made in the page, so no media file is needed.
7 """
8 import http.server, pathlib
9 
10 ROOT = pathlib.Path(__file__).resolve().parent.parent.parent
11 PAGE = b"""<!doctype html><meta charset=utf-8><title>mock watch page</title>
12 <link rel=stylesheet href=/src/content.css>
13 <body style="margin:0;background:#111">
14 <div id=movie_player style="position:relative;width:960px;height:540px;background:#000;overflow:hidden">
15   <video style="width:100%;height:100%" muted loop></video>
16 </div>
17 <script>
18   // A silent moving picture from a canvas: a video element with a real clock.
19   const c = document.createElement('canvas'); c.width = 320; c.height = 180;
20   const g = c.getContext('2d'); let n = 0;
21   setInterval(() => { g.fillStyle = '#123'; g.fillRect(0, 0, 320, 180); g.fillStyle = '#fff'; g.fillText(String(n++), 10, 20); }, 100);
22   const v = document.querySelector('video'); v.srcObject = c.captureStream(10); v.play();
23 </script>
24 <script src=/src/srt.js></script><script src=/src/content.js></script>
25 <script>
26   // ?demo : subtitles loaded and the panel open, for a screenshot.
27   if (location.search.includes('demo')) setTimeout(() => {
28     const srt = '1\\n00:00:00,000 --> 09:00:00,000\\n\\u543e\\u8f29\\u306f\\u732b\\u3067\\u3042\\u308b\\u3002\\u540d\\u524d\\u306f\\u307e\\u3060\\u7121\\u3044\\u3002\\n';
29     const input = document.querySelector('.subread-file'), dt = new DataTransfer();
30     dt.items.add(new File([srt], 'wagahai.ja.srt')); input.files = dt.files; input.dispatchEvent(new Event('change'));
31     document.querySelector('.subread-button').click();
32   }, 800);
33 </script>"""
34 
35 class Handler(http.server.SimpleHTTPRequestHandler):
36     def __init__(self, *a, **k): super().__init__(*a, directory=str(ROOT), **k)
37     def do_GET(self):
38         if self.path.split('?')[0] == '/watch':
39             self.send_response(200); self.send_header('Content-Type', 'text/html'); self.end_headers(); self.wfile.write(PAGE)
40         else: super().do_GET()
41 
42 http.server.ThreadingHTTPServer(('127.0.0.1', 8432), Handler).serve_forever()