2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
11 * Default media scraper
13 * Looks for <embed> and <object> elements, and also uses regular
14 * expresisons to attempt to match strings that look like URIs to
18 define_variable("media_scrape_default_regexp",
19 new RegExp("(?:http://[a-zA-Z0-9.\\-]+/)?(?!://)[^=&<>\"'|\\s]+\\."
20 + "(?:aiff|au|avi|flv|mid|mov|mp3|mpg|mpeg|ogg|ra|rm|spl|wav|wma|wmv)(?!\\w)", "ig"),
21 "Regular expression used by the default media scraper to match URIs for "
22 + "embedded media in the page source code.");
24 function media_scrape_default (buffer, results) {
25 var initial_length = results.length;
26 for (let frame in frame_iterator(buffer.top_frame)) {
27 var text = frame.document.documentElement.innerHTML;
28 var matches = text.match(media_scrape_default_regexp);
29 //matches = matches.concat(unescape(text).match(media_scrape_default_regexp));
31 let base_uri = frame.document.documentURIObject;
33 var uris = new string_hashset();
34 for each (let x in matches) {
37 let uri_obj = make_uri(str, null, base_uri);
38 if (!uris.contains(uri_obj.spec)) {
39 uris.add(uri_obj.spec);
40 results.push(load_spec({uri: uri_obj.spec, source_frame: frame}));
46 // If there is exactly 1, use the document title as the video title
47 if (initial_length == 0 && results.length == 1 &&
48 buffer.document.title && buffer.document.title.length > 0)
50 results[0].title = buffer.document.title;
56 define_variable("media_scrapers",
57 [[/.*/, media_scrape_default]],
58 "Associative list of regexps for matching urls, mapped to functions "+
59 "(or coroutines) to use to scrape a page for embedded media.")
62 function media_scrape (I, buffer) {
64 for (var i = 0, nscrapers = media_scrapers.length; i < nscrapers; i++) {
65 if (media_scrapers[i][0].test(buffer.current_uri.spec))
66 yield media_scrapers[i][1](buffer, results);
68 yield co_return(results);
72 define_browser_object_class("media", null,
73 function (I, prompt) {
74 let media = yield media_scrape(I, I.buffer);
75 if (!media || media.length == 0)
76 throw interactive_error("No media found.");
77 if (media.length == 1)
78 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+")" : "");
90 let result = yield I.buffer.window.minibuffer.read(
93 $completer = completer,
94 $auto_complete_initial,
95 $auto_complete = "media");
96 yield co_return(result);
98 $hint = "select media");
99 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
106 buffer.default_browser_object_classes['shell-command-on-file'] =
107 browser_object_media;
108 buffer.default_browser_object_classes['shell-command-on-url'] =
109 browser_object_media;