]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LinkPreview/linkpreview.js
LinkPreview: flesh out stub JS code a bit. URL splitting doesn't quite match core...
[quix0rs-gnu-social.git] / plugins / LinkPreview / linkpreview.js
1 /**
2  * (c) 2010 StatusNet, Inc.
3  */
4
5 $(function() {
6     /**
7      * Find URL links from the source text that may be interesting.
8      *
9      * @param {String} text
10      * @return {Array} list of URLs
11      */
12     function findLinks(text)
13     {
14         // @fixme match this to core code
15         var re = /(?:^| )(https?:\/\/.+?\/.+?)(?= |$)/g;
16         var links = [];
17         var matches;
18         while ((matches = re.exec(text)) !== null) {
19             links.push(matches[1]);
20         }
21         return links;
22     }
23
24     /**
25      * Start looking up info for a link preview...
26      * May start async data loads.
27      *
28      * @param {String} id
29      * @param {String} url
30      */
31     function prepLinkPreview(id, url)
32     {
33         console.log(id, url);
34     }
35
36     /**
37      * Update the live preview section with links found in the given text.
38      * May start async data loads.
39      *
40      * @param {String} text: free-form input text
41      */
42     function previewLinks(text)
43     {
44         var links = findLinks(text);
45         for (var i = 0; i < links.length; i++) {
46             var id = 'link-preview-' + i;
47             prepLinkPreview(id, links[i]);
48         }
49     }
50
51     $('#notice_data-text').change(function() {
52        var text = $(this).val();
53        previewLinks(text);
54     });
55 });