Skip to content

Instantly share code, notes, and snippets.

@iFwu
Last active May 21, 2019 11:46
Show Gist options
  • Save iFwu/f20acc4fc454a3cd6921e52d9bea011c to your computer and use it in GitHub Desktop.
Save iFwu/f20acc4fc454a3cd6921e52d9bea011c to your computer and use it in GitHub Desktop.
从煎蛋标题找到文章链接的阿里云函数计算服务
const { promisifyAll, promisify } = require('bluebird');
const request = promisify(require('request'));
promisifyAll(request, { multiArgs: true });
const cheerio = require('cheerio');
module.exports.handler = async function(req, resp, context) {
const articleTitle = req.queries.articleTitle;
console.log(articleTitle);
const commentId = req.queries.commentId;
const commentAnchor = commentId ? `#comment-${commentId}` : '';
const { body, statusCode, headers } = await request({
url: `https://www.google.com.hk/search?q=${encodeURIComponent(
articleTitle + ' site:jandan.net',
)}&btnI=&gws_rd=cr`,
followRedirect: false,
encoding: 'utf-8',
headers: {
'user-agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
},
});
resp.setStatusCode(302);
resp.setHeader('content-type', 'text/html;charset=utf-8');
let articleUrl
if (statusCode !== 200) {
articleUrl = headers.location;
} else {
const $ = cheerio.load(body);
const a = $('.srg .g')
.first()
.find('a');
articleUrl = a.attr('href');
}
articleUrl += commentAnchor;
resp.setHeader('location', articleUrl);
const html = `<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Page Moved</title></head><body>
<h1>Page Moved</h1>
The document has moved
<a href="${articleUrl}">here</a>.
</body></html>`;
resp.send(html);
};
// Tampermonkey Script
(function () {
'use strict';
$('#list-comment .acv_author a').each(function (_, a) {
var href = a.href;
var commentId = href.split('#')[1];
var title = a.text;
a.href = 'https://1839151804334651.cn-hongkong.fc.aliyuncs.com/2016-08-15/proxy/jandan/redirect-top-comment-article/?articleTitle=' + encodeURIComponent(title) + '&commentId=' + commentId;
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment