Skip to content

Instantly share code, notes, and snippets.

@lelinhtinh
Last active September 28, 2018 06:51
Show Gist options
  • Save lelinhtinh/4914bbd7fb23f9e9b39d2b4f5bc609e4 to your computer and use it in GitHub Desktop.
Save lelinhtinh/4914bbd7fb23f9e9b39d2b4f5bc609e4 to your computer and use it in GitHub Desktop.
(function () {
'use strict';
if (location.host !== 'daynhauhoc.com') {
const checkHost = confirm('Bookmarklet does not work on this page.\nDo you want to open DayNhauHoc?');
if (checkHost) top.location.href = 'https://daynhauhoc.com';
return;
}
if (!Discourse.User.current()) {
alert('You are not logged in!\nPlease log in and try again.');
return;
}
var listPosts = [],
listPostsSize = 0,
currentIndex = 0,
likedCount = 0,
title = document.title,
$win = $(window),
pathname = location.pathname,
progressing = () => {
$win.on('beforeunload', function () {
return 'Progress is running...';
});
listPosts = [];
listPostsSize = 0;
currentIndex = 0;
likedCount = 0;
},
likePost = () => {},
nextPost = (stop = false, err) => {
currentIndex++;
if (!stop && currentIndex < listPostsSize) {
document.title = `[${likedCount}/${currentIndex}] ${title}`;
likePost();
} else {
$win.off('beforeunload');
document.title = title;
delete window.dnhAutoLikeIsRunning;
let mess = '';
if (err && err.responseJSON && err.responseJSON.errors) mess = err.responseJSON.errors[0] + '\n\n';
alert(`${mess}Liked: ${likedCount}/${listPostsSize}`);
}
},
liking = data => {
if (!(data && data.actions_summary)) return;
const postId = data.id,
likeAction = data.actions_summary.find(x => x.id === 2);
if (!(likeAction && likeAction.can_act)) {
console.log('Ignore', `https://daynhauhoc.com/p/${postId}`);
nextPost();
return;
}
$.post('/post_actions', {
id: postId,
post_action_type_id: 2
}).done(() => {
console.log('Liked', `https://daynhauhoc.com/p/${postId}`);
likedCount++;
}).fail(() => {
console.log('Erros', `https://daynhauhoc.com/p/${postId}`);
}).always(res => {
nextPost((res.status === 429), res);
});
};
function autoLikeUser(username, offset = 0) {
if (window.dnhAutoLikeIsRunning) return;
window.dnhAutoLikeIsRunning = true;
progressing();
likePost = () => {
$.get(`/posts/${listPosts[currentIndex]}`).done(data => {
liking(data);
}).fail(err => {
nextPost(true, err);
});
};
$.getJSON(`/user_actions.json?offset=${offset}&username=${username}&filter=4,5`).done(data => {
if (!(data && data.user_actions)) return;
$.each(data.user_actions, (i, v) => {
if (v.post_id) listPosts.push(v.post_id);
});
listPostsSize = listPosts.length;
likePost();
}).fail(err => {
nextPost(true, err);
});
}
function autoLikeTopic(topicUrl) {
if (window.dnhAutoLikeIsRunning) return;
window.dnhAutoLikeIsRunning = true;
progressing();
likePost = () => {
liking(listPosts[currentIndex]);
};
const topicId = topicUrl.match(/\/t\/[^/]+\/(\d+)/)[1];
$.getJSON(`/t/${topicId}.json`).done(data => {
if (!(data && data.post_stream)) return;
listPosts = data.post_stream.posts;
listPostsSize = listPosts.length;
likePost();
}).fail(err => {
nextPost(true, err);
});
}
if (pathname.indexOf('/u/') === 0) {
autoLikeUser(pathname.match(/^\/u\/([^/]+)/)[1]);
} else if (pathname.indexOf('/t/') === 0) {
autoLikeTopic(pathname);
} else {
let username = prompt('Please enter your idol username:', 'drgnz');
if (username) autoLikeUser(username);
}
$(document).on('contextmenu', 'a[data-user-card]', e => {
if (!e.altKey) return;
e.preventDefault();
let offset = 0;
if (e.shiftKey) offset = prompt('Set offset of the posts list:', 30) || 30;
autoLikeUser(e.currentTarget.dataset.userCard, offset);
}).on('contextmenu', 'a[href^="/t/"]', e => {
if (!e.altKey) return;
e.preventDefault();
autoLikeTopic(e.currentTarget.href);
});
})();
@lelinhtinh
Copy link
Author

lelinhtinh commented May 22, 2018

Bookmarklet

https://jsfiddle.net/baivong/4avawtyk/show/

Cách dùng

Kéo thả Bookmarklet lên thanh Bookmark của trình duyệt và click để sử dụng, hoặc chạy script ở trên trong Console.

Like theo user

  • Chạy trực tiếp nếu đang ở trang profile. Ở trang khác sẽ xuất hiện prompt để nhập username.
  • Giữ AltRight-click trên user link.
  • Like 30 bài viết đầu tiên. Đối với link có thể giữ thêm Shift để chọn vị trí bắt đầu trong danh sách bài viết.

Like theo topic

  • Chạy trực tiếp nếu đang ở trong topic.
  • Giữ AltRight-click trên topic link.
  • Like 20 bài viết đầu tiên.

Chú ý

Một account chỉ có thể like tối đa 50 lần trong 24h.

@neihousaigaai
Copy link

Cần thêm tính năng thông báo user không hợp lệ :x

@lelinhtinh
Copy link
Author

Kiểm tra /u/USERNAME.json có vẻ ổn, nhưng mà mất công tải quá. 🤔
Hay là giữ Alt khi right-click vào user đi, không sợ sai, khỏi cần kiểm tra. 😬

@Aragami1408
Copy link

Cảm ơn anh nha! Thế này không lo ngại like nữa rồi 😈

@taisang1996
Copy link

Hay quá anh ơi. cho em xin facebook liên lạc với
fb.com (at) taisang1996

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