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 (I, buffer) {
59 var scrapers = I.local.media_scrapers;
62 for each (let scraper in scrapers) {
63 yield scraper(buffer, results);
66 yield co_return(results);
70 define_browser_object_class("media", null,
71 function (I, prompt) {
72 let media = yield media_scrape(I, I.buffer);
73 if (!media || media.length == 0)
74 throw interactive_error("No media found.");
76 if (media.length == 1)
77 yield co_return(media[0]);
79 let completer = all_word_completer(
81 $get_string = function (x) {
83 return load_spec_uri_string(x);
85 $get_description = function (x) {
87 return (load_spec_title(x) || "") +
88 (x.description ? " ("+x.description+")" : "");
91 let result = yield I.buffer.window.minibuffer.read(
94 $completer = completer,
95 $auto_complete_initial,
96 $auto_complete = "media");
98 yield co_return(result);
100 minibuffer_auto_complete_preferences["media"] = true;
102 function media_setup_local_object_classes(buffer) {
103 buffer.default_browser_object_classes = {
104 save: browser_object_media };
105 buffer.default_browser_object_classes['shell-command-on-file'] =
106 browser_object_media;
107 buffer.default_browser_object_classes['shell-command-on-url'] =
108 browser_object_media;