Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
同时更新多个微博状态服务(Twitter/新浪微博/网易微博)
const TWEET_MAXLEN = 140;
const TWEET_SHORT_URL_MAX_LENGTH = 17;
const URL_SHORTENER_API = 'http://api.bit.ly/shorten?version=2.0.1&format=text&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&longUrl=';
var MultiStatusServicesUpdater = {
registeredServices: [
{ selector: '#TWITTER', method: 'updateTwitter' },
// { selector: '#KAIXIN', method: 'updateKaixin' },
{ selector: '#SINA', method: 'updateSina' },
// { selector: '#QQ', method: 'updateQQ' },
{ selector: '#163', method: 'update163' },
{ selector: '#TEST', method: 'updateTest' },
],
getAllServices: function() {
var actions = [];
for (var i = 0; i < this.registeredServices.length; i++)
if (this.registeredServices[i].selector != '#TEST')
actions.push(this.registeredServices[i].selector);
return actions;
},
parseActions: function(finalStatusText) {
var actions = [];
for (var i = 0; i < this.registeredServices.length; i++) {
var service = this.registeredServices[i];
if (finalStatusText.indexOf(' ' + service.selector) != -1) {
finalStatusText = finalStatusText.replace(new RegExp(' ' + service.selector), '');
actions.push(service.selector);
}
}
if (actions.length == 0) actions = this.getAllServices();
return { 'status': finalStatusText, 'actions': actions };
},
updateStatus: function(finalStatusText) {
var result = this.parseActions(finalStatusText);
this.processUpdate(result.status, result.actions, '', '');
},
processUpdate: function(finalStatusText, actions, successServices, failServices) {
if (actions.length <= 0) {
var msg = '';
if (failServices != '') msg = msg + ' [FAIL]' + failServices;
if (successServices != '') msg = msg + ' [Success]' + successServices;
msg = msg + ', [Status]: ' + finalStatusText;
displayMessage(msg);
return; // no action to be processed
}
var action = actions.shift();
var thisObj = this;
var cb = function(serviceName, status) { // process the next action
if (status == 'SUCCESS')
thisObj.processUpdate(finalStatusText, actions, successServices + ' ' + serviceName, failServices);
else
thisObj.processUpdate(finalStatusText, actions, successServices, failServices + ' ' + serviceName);
};
for (var i = 0; i < this.registeredServices.length; i++) {
var service = this.registeredServices[i];
if (action == service.selector) {
this[service.method](finalStatusText, cb, service.selector);
return;
}
}
displayMessage('Unknown action: ' + action);
if (cb) cb(action, 'FAIL');
},
updateTest: function(finalStatusText, cb, serviceSelector) {
if (cb) cb(serviceSelector, 'SUCCESS');
},
updateTwitter: function(finalStatusText, cb, serviceSelector) {
var updateUrl = "https://twitter.com/statuses/update.json";
var updateParams = { source: 'Ubiquity', status: finalStatusText };
jQuery.post(updateUrl, updateParams, function() { if (cb) cb(serviceSelector, 'SUCCESS'); }, 'json');
},
update163: function(finalStatusText, cb, serviceSelector) {
var updateUrl = "http://t.163.com/statuses/update.json";
var updateParams = { source: "Ubiquity", status: finalStatusText };
jQuery.post(updateUrl, updateParams, function() { if (cb) cb(serviceSelector, 'SUCCESS'); }, 'json');
},
genericUpdate: function(serviceSelector, homeUrl, homeTitle, updateUrl, updateParams, updateDataType, callback, refererUrl) {
if (refererUrl == null) refererUrl = homeUrl;
jQuery.get(homeUrl, function(data, textStatus) {
if (data.indexOf(homeTitle) >= 0) { // home title found => already logged in
jQuery.ajax({
type: "POST",
url: updateUrl,
data: updateParams,
dataType: updateDataType,
error: function() { if (callback) callback(serviceSelector, 'FAIL'); },
success: function(data, textStatus) { if (callback) callback(serviceSelector, 'SUCCESS'); },
beforeSend: function(req) {
req.withCredentials = true;
req.setRequestHeader('Referer', refererUrl);
},
});
} else { // not yet logged in
displayMessage(serviceSelector + ': not yet logged in!');
if (callback) callback(serviceSelector, 'FAIL');
}
});
},
updateKaixin: function(finalStatusText, cb, serviceSelector) {
var homeUrl = 'http://www.kaixin001.com/home/';
var homeTitle = '<title>我的首页';
var updateUrl = 'http://www.kaixin001.com/friend/status_submit.php?rnd=' + Math.random();
var updateParams = { state: finalStatusText, };
var updateDataType = 'html';
this.genericUpdate(serviceSelector, homeUrl, homeTitle, updateUrl, updateParams, updateDataType, cb);
},
updateSina: function(finalStatusText, cb, serviceSelector) {
var homeUrl = 'http://login.sina.com.cn/cgi/login/home.php';
var homeTitle = '<div class="center_main">';
var updateUrl = "http://t.sina.com.cn/mblog/publish.php?rnd=" + Math.random();
var updateParams = { content: finalStatusText, from: 'myprofile' };
var updateDataType = 'json';
var refererUrl = 'http://t.sina.com.cn/mblog/';
this.genericUpdate(serviceSelector, homeUrl, homeTitle, updateUrl, updateParams, updateDataType, cb, refererUrl);
},
update163_deprecated: function(finalStatusText, cb, serviceSelector) {
var homeUrl = 'http://t.163.com/home';
var homeTitle = '<title>我的首页';
var updateUrl = "http://t.163.com/statuses/update.do";
var updateParams = { status: finalStatusText, in_reply_to_status_id: '' };
var updateDataType = 'json';
this.genericUpdate(serviceSelector, homeUrl, homeTitle, updateUrl, updateParams, updateDataType, cb);
},
updateQQ: function(finalStatusText, cb, serviceSelector) {
var homeUrl = 'http://www.taotao.com/v1/mydaoke';
var homeTitle = '<title>滔滔-我的叨客';
var updateUrl = "http://www.taotao.com/v1/mydaoke/post?rnd=" + Math.random() + "&synqqsign=1&content=" + encodeURIComponent(finalStatusText);
var updateParams = null;
var updateDataType = 'html';
this.genericUpdate(serviceSelector, homeUrl, homeTitle, updateUrl, updateParams, updateDataType, cb);
},
};
CmdUtils.CreateCommand({
name: ['t', 'twitter', 'tweet'],
icon: "http://assets3.twitter.com/images/favicon.ico",
arguments: [ {role: 'status', nountype: noun_arb_text, label: 'query'} ],
homepage: "http://twitter.com/ziru",
description: "发布更新至Twitter/Meme/开心网/新浪微博/腾讯滔滔.",
help: "使用此命令更新状态之前请先登录相应微博服务",
preview: function(pblock, args) {
var statusText = args.status.text;
var result = MultiStatusServicesUpdater.parseActions(statusText);
var statusLength = result.status.length;
var usesUrlShortener = statusText.indexOf('#URL') != -1;
if (usesUrlShortener) statusLength += TWEET_SHORT_URL_MAX_LENGTH - 4;
var previewData = {
status: result.status,
chars: TWEET_MAXLEN - statusLength
};
var previewTemplate = "更新微博状态为: <br /><br /><b>${status}</b><br /><br />剩余字符数: <b>${chars}</b>";
var truncateTemplate = "<br />最后<b>${truncate}</b>个字符将被移除!";
var shortenInstructionText = "<br />如需添加当前页面链接, 请在状态中加入<b>#URL</b>.";
var selectorInstructionText = "<br />如仅需更新部分微博服务, 请在状态中加入相应选择符:<b>" + MultiStatusServicesUpdater.getAllServices().join(', ') + "</b>.";
var urlWillBeShortenedText = "<br /><b>#URL</b>部分将会替换为当前页面链接(使用bit.ly服务压缩).";
var actionsTemplate = "<br /><br />状态信息将被更新到以下微博服务: <b>${services}</b>.";
var previewHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
if (usesUrlShortener) previewHTML += urlWillBeShortenedText;
else previewHTML += shortenInstructionText;
previewHTML += selectorInstructionText;
if (previewData.chars < 0)
previewHTML += CmdUtils.renderTemplate(truncateTemplate, { truncate: 0 - previewData.chars });
previewHTML += CmdUtils.renderTemplate(actionsTemplate, {services: result.actions.join(', ').replace(/#/g, '')});
pblock.innerHTML = previewHTML;
},
execute: function(args) {
jQuery.ajaxSetup({
error: function(req, textStatus) {
if (req.status == 0) {
displayMessage('No network error');
} else if (req.status == 404) {
displayMessage('Requested URL not found');
} else if (req.status == 500) {
displayMessage('Internel server error');
} else if (textStatus == 'parsererror') {
displayMessage('Failed parsing JSON response');
} else if (textStatus == 'timeout') {
displayMessage('Request time out');
} else {
displayMessage('Unknow error: ' + req.responseText);
}
},
});
var statusText = args.status.text;
if(statusText.length < 1) return;
var usesUrlShortener = statusText.indexOf(' #URL') != -1;
var thisObj = this;
var postUpdate = function( shortURL ) {
var finalStatusText = statusText.replace('#URL', shortURL);
MultiStatusServicesUpdater.updateStatus(finalStatusText);
}
if (usesUrlShortener) {
var urlToShorten = Application.activeWindow.activeTab.document.location.href;
jQuery.get( URL_SHORTENER_API + encodeURIComponent(urlToShorten), null, postUpdate);
} else {
postUpdate('');
}
},
});
@heytxz

This comment has been minimized.

Copy link

@heytxz heytxz commented Aug 26, 2017

不懂编程真是寂寞如雪

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