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;
51 results[0].suggest_filename_from_uri = false;
57 define_variable("media_scrapers",
58 [[/.*/, media_scrape_default]],
59 "Associative list of regexps for matching urls, mapped to functions "+
60 "(or coroutines) to use to scrape a page for embedded media.")
63 function media_scrape (I, buffer) {
65 for (var i = 0, nscrapers = media_scrapers.length; i < nscrapers; i++) {
66 if (media_scrapers[i][0].test(buffer.current_uri.spec))
67 yield media_scrapers[i][1](buffer, results);
69 yield co_return(results);
73 define_browser_object_class("media", null,
74 function (I, prompt) {
75 let media = yield media_scrape(I, I.buffer);
76 if (!media || media.length == 0)
77 throw interactive_error("No media found.");
78 if (media.length == 1)
79 yield co_return(media[0]);
80 let completer = all_word_completer(
82 $get_string = function (x) {
84 return load_spec_uri_string(x);
86 $get_description = function (x) {
88 return (load_spec_title(x) || "") +
89 (x.description ? " ("+x.description+")" : "");
91 let result = yield I.buffer.window.minibuffer.read(
94 $completer = completer,
95 $auto_complete_initial,
96 $auto_complete = "media");
97 yield co_return(result);
99 $hint = "select media");
100 minibuffer_auto_complete_preferences["media"] = true;
103 function media_setup_local_object_classes (buffer) {
104 buffer.default_browser_object_classes = {
105 save: browser_object_media
107 buffer.default_browser_object_classes['shell-command-on-file'] =
108 browser_object_media;
109 buffer.default_browser_object_classes['shell-command-on-url'] =
110 browser_object_media;