index file with bookmarklet
[stapibas.git] / www / js / show-links.js
1 /**
2  * Load it with:
3  * s=document.createElement('script');s.src='http://stapibas.bogo/js/show-links.js';document.body.appendChild(s);
4  */
5 var scripts = document.getElementsByTagName("script");
6 var thisScript = scripts[scripts.length-1];
7 var thisScriptsSrc = thisScript.src;
8 var stapibasUrl = thisScriptsSrc.replace('js/show-links.js', '');
9 //var stapibasUrl = 'http://stapibas.bogo/';
10
11 var pageUrl = window.location.href;
12
13 function loadScript(url, callback)
14 {
15     var script = document.createElement('script');
16     script.type = 'text/javascript';
17     script.src = url;
18     script.onreadystatechange = callback;
19     script.onload = callback;
20     document.getElementsByTagName('head')[0].appendChild(script);
21 }
22
23 function loadData()
24 {
25     jQuery('head').append(
26         '<link rel="stylesheet" type="text/css" href="'
27         + stapibasUrl + 'css/show-links.css" />'
28         + '<link rel="stylesheet" type="text/css" href="'
29         + stapibasUrl + 'css/jquery-0.6.1.smallipop.min.css" />'
30     );
31     jQuery.ajax(
32         stapibasUrl + 'api/links.php?url='
33         + encodeURIComponent(fixUrl(pageUrl))
34     ).done(function(data) {showData(data);})
35         .fail(function(data) {showError(data);});
36 }
37
38 function showData(data)
39 {
40     var items = jQuery('.e-content a');
41     if (items.length == 0) {
42         items = jQuery('a');
43     }
44
45     //collect stats
46     var stats = {
47         links: 0
48     };
49     $.each(data.links, function(key, link) {
50         stats.links++;
51         if (!stats[link.status]) {
52             stats[link.status] = 1;
53         } else {
54             stats[link.status]++;
55         }
56     });
57     var statlist = '';
58     $.each(stats, function(status, count) {
59         statlist = statlist + '<li>' + count + ' ' + status + '</li>';
60     });
61     $('body').prepend(
62         '<div class="stapibas-stats">'
63             + '<b>Linkback stats</b>: '
64             + '<ul>' + statlist + '</ul>'
65             + '</div>'
66     );
67
68     //add link info
69     items.each(function(key, elem) {
70         if (!data.links[fixUrl(elem.href)]) {
71             return;
72         }
73         var link = data.links[fixUrl(elem.href)];
74         $(elem).addClass('stapibas-link')
75             .addClass('stapibas-status-' + link.status);
76         $(elem).smallipop(
77             {
78                 theme: 'white'
79             },
80             '<h2>Linkback information</h2>'
81                 + '<dl>'
82                 + '<dt>Status</dt><dd>' + link.status + '</dd>'
83                 + '<dt>Pinged</dt><dd>' + link.pinged + '</dd>'
84                 + '<dt>Updated</dt><dd>' + link.updated + '</dd>'
85                 + '<dt>Tries</dt><dd>' + link.tries + '</dd>'
86                 + '<dt>Error code</dt><dd>' + link.error.code + '</dd>'
87                 + '<dt>Error message</dt><dd>' + link.error.message + '</dd>'
88                 + '</dl>'
89         );
90     });
91 }
92
93 function fixUrl(url)
94 {
95     return url.replace(/www.bogo/, 'cweiske.de');
96 }
97
98 function showError(data)
99 {
100     $('body').prepend(
101         '<div class="stapibas-stats stapibas-stats-error">'
102             + 'Error loading linkback data: '
103             + data.status + ' <b>' + data.statusText + '</b>'
104             + '</div>'
105     );
106 }
107
108 loadScript(
109     stapibasUrl + 'js/jquery-2.1.0.js',
110     function() {
111         loadScript(stapibasUrl + 'js/jquery-0.6.1.smallipop.js', loadData);
112     }
113 );