forked from shstkvch/minishare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minishare-0.0.1.js
93 lines (80 loc) · 2.84 KB
/
minishare-0.0.1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/********************************
MiniShare - Simple Sharing Widget for jQuery
v0.0.1
Copyright (C) 2013 David Hewitson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************/
(function($) {
$.miniShare = function(options) {
if($('#minishare').length > 0) {
if (window.console && window.console.error) {
console.error('Minishare widget already on the page (or there\'s a naming conflict.)');
}
return false;
}
var def_html =
[ "<div id=\"minishare\">"
, "<p id=\"message\">Share</p>"
, "<div class=\"facebook\">"
, "<a href=\"#\">Share</a>"
, "</div>"
, "<div class=\"twitter\">"
, "<a href=\"#\">Tweet</a>"
, "</div>"
, "</div>"
].join('');
var services = {
twitter: "https://twitter.com/intent/tweet?text=",
facebook: "https://www.facebook.com/sharer/sharer.php?u="
};
var defaults = {
url: document.location,
message: "Share This",
done_message: "Thanks!",
twitter_message: 'Check this out', // article title or something
html: def_html,
style: './css/minishare.css'
}
var opts = $.extend(defaults, options);
// inject the MiniShare widget
widget = $('body').append(opts.html);
// inject the css
$('head').append('<link rel="stylesheet" id="minishare-styles" href="' + opts.style + '"></link>');
// set the message
$('#minishare p#message').html(opts.message);
$('#minishare div').click(function() {
$(this).children()[0].click(); // quick & dirty fix
});
$('#minishare a').click(function() {
var parent_class = $(this).parent().attr('class');
if(services[parent_class] == undefined) {
return false; // SANITY CHEKKKKKKKCKCK
};
var the_url = '';
if(parent_class == "twitter") { // add a wee message for twitter users
message = opts.twitter_message;
the_url = services[parent_class] + opts.twitter_message + "&url=" + opts.url;
} else {
the_url = services[parent_class] + opts.url;
};
var share_window = window.open(the_url, 'sharer', 'width=600,height=400,top=200,left=200');
var watchClose = setInterval(function() {
try {
if (share_window.closed) {
clearTimeout(watchClose);
$('#minishare p#message').html(opts.done_message)
}
} catch (e) {}
}, 200);
});
};
})(jQuery);