Skip to content

Instantly share code, notes, and snippets.

@saippuakauppias
Created September 1, 2015 10:43
Show Gist options
  • Save saippuakauppias/247af51c6ed50503afe4 to your computer and use it in GitHub Desktop.
Save saippuakauppias/247af51c6ed50503afe4 to your computer and use it in GitHub Desktop.
Share text or link in vk (vkontakte, vk.com), fb (facebook, facebook.com), tw (twitter, twitter.com)
/********************************************************************
*
* Share social http://habrahabr.ru/post/156185/
*
*********************************************************************/
function Share(purl, ptitle, pimg, text) {
'use strict';
this.purl = purl;
this.ptitle = ptitle;
this.pimg = pimg;
this.text = text;
/*
url Ссылка на страницу, которая будет публиковаться.
title Заголовок публикации. Если не указан, то будет браться со страницы публикации.
description Описание публикации. Если не указано, то будет браться со страницы публикации.
image Ссылка на иллюстрацию к публикации. Если не указана, то будет браться со страницы публикации.
*/
this.vk = function () {
var url = 'http://vk.com/share.php?';
if (this.purl) {
url += 'url=' + encodeURIComponent(this.purl);
}
if (this.ptitle) {
url += '&title=' + encodeURIComponent(this.ptitle);
}
if (this.pimg) {
url += '&image=' + encodeURIComponent(this.pimg);
}
url += '&noparse=true';
this.popup(url);
};
this.fb = function () {
var url = 'http://www.fb.com/sharer.php?s=100';
if (this.ptitle) {
url += '&p[title]=' + encodeURIComponent(this.ptitle);
}
if (this.text) {
url += '&p[summary]=' + encodeURIComponent(this.text);
}
if (this.purl) {
url += '&p[url]=' + encodeURIComponent(this.purl);
}
if (this.pimg) {
url += '&p[images][0]=' + encodeURIComponent(this.pimg);
}
this.popup(url);
};
this.tw = function () {
var url = "https://twitter.com/intent/tweet?",
MAX_LEN_TW = 140,
content = this.ptitle,
site_url = this.purl,
index = MAX_LEN_TW;
while ((content + site_url).length > MAX_LEN_TW) {
index = content.lastIndexOf(' ', index - 1);
if (index !== -1 && index - 4 - site_url.length <= MAX_LEN_TW) {
content = content.slice(0, index);
content += '... ';
} else if (index === -1) {
content = '';
}
}
if (this.purl) {
url += "original_referer=" + encodeURIComponent(this.purl);
}
if (this.ptitle) {
url += "&text=" + encodeURIComponent(content);
}
url += "&tw_p=tweetbutton";
if (this.purl) {
url += "&url=" + encodeURIComponent(site_url);
}
this.popup(url);
};
this.popup = function (url) {
window.open(url, '', 'toolbar=0,status=0,width=626,height=436');
};
}
@huanting
Copy link

huanting commented Jun 19, 2023

vk分享,可以不传入不传入标题和图片,这种情况会使用是url中的Open Graph metadata数据展示。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment