2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
9 * Default media scraper
11 * Looks for <embed> and <object> elements, and also uses regular
12 * expresisons to attempt to match strings that look like URIs to
16 define_variable("media_scrape_default_regexp",
17 new RegExp("(?:http://[a-zA-Z0-9.\\-]+/)?(?!://)[^=&<>\"'|\\s]+\\."
18 + "(?:aiff|au|avi|flv|mid|mov|mp3|mpg|mpeg|ogg|ra|rm|spl|wav|wma|wmv)(?!\\w)", "ig"),
19 "Regular expression used by the default media scraper to match URIs for "
20 + "embedded media in the page source code.");
22 function media_scrape_default(buffer, results) {
23 var initial_length = results.length;
24 for (let frame in frame_iterator(buffer.top_frame)) {
25 var text = frame.document.documentElement.innerHTML;
26 var matches = text.match(media_scrape_default_regexp);
27 //matches = matches.concat(unescape(text).match(media_scrape_default_regexp));
29 let base_uri = frame.document.documentURIObject;
31 var uris = new string_hashset();
32 for each (let x in matches) {
35 let uri_obj = make_uri(str, null, base_uri);
36 if (!uris.contains(uri_obj.spec)) {
37 uris.add(uri_obj.spec);
38 results.push(load_spec({uri: uri_obj.spec, source_frame: frame}));
44 // If there is exactly 1, use the document title as the video title
45 if (initial_length == 0 && results.length == 1 &&
46 buffer.document.title && buffer.document.title.length > 0) {
48 results[0].title = buffer.document.title;
49 results[0].suggest_filename_from_uri = false;
54 define_variable("media_scrapers", [media_scrape_default],
55 "Function (or coroutine) to use to scrape the current buffer for embedded media.");
58 function media_scrape(buffer) {
59 var scrapers = buffer.get("media_scrapers");
62 for each (let scraper in scrapers) {
63 yield scraper(buffer, results);
66 yield co_return(results);
69 function media_setup_local_object_classes(buffer) {
70 buffer.local_variables.default_browser_object_classes = {
71 __proto__: default_browser_object_classes,
73 shell_command: "media",
74 shell_command_url: "media"
78 define_browser_object_class("media", $handler = function (buf, prompt) {
79 let media = yield media_scrape(buf);
80 if (!media || media.length == 0)
81 throw interactive_error("No media found.");
83 if (media.length == 1)
84 yield co_return(media[0]);
86 let completer = all_word_completer(
88 $get_string = function (x) load_spec_uri_string(x),
89 $get_description = function (x) load_spec_title(x) || "");
91 let result = yield buf.window.minibuffer.read(
94 $completer = completer,
95 $auto_complete_initial,
96 $auto_complete = "media");
98 yield co_return(result);