Last active
October 23, 2017 15:42
-
-
Save jangxx/a0b7f9b4f9eb37d0198955caf971afef to your computer and use it in GitHub Desktop.
Userscript for Tampermonkey to add videos from reddit to a playlist on sync-video.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Better SyncVideo | |
// @namespace http://jangxx.com | |
// @version 0.6 | |
// @description Adds various improvements to sync-video.com, for example reddit integration | |
// @author jangxx | |
// @match *://www.sync-video.com/r/* | |
// @match *://sync-video.com/r/* | |
// @grant GM_xmlhttpRequest | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @grant unsafeWindow | |
// @connect reddit.com | |
// @downloadURL https://gist.github.com/jangxx/a0b7f9b4f9eb37d0198955caf971afef/raw/bettersyncvideo.user.js | |
// @updateURL https://gist.github.com/jangxx/a0b7f9b4f9eb37d0198955caf971afef/raw/bettersyncvideo.meta.js | |
// @require https://userscripts-mirror.org/scripts/source/107941.user.js | |
// ==/UserScript== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Better SyncVideo | |
// @namespace http://jangxx.com | |
// @version 0.6 | |
// @description Adds various improvements to sync-video.com, for example reddit integration | |
// @author jangxx | |
// @match *://www.sync-video.com/r/* | |
// @match *://sync-video.com/r/* | |
// @grant GM_xmlhttpRequest | |
// @grant GM_setValue | |
// @grant GM_getValue | |
// @grant unsafeWindow | |
// @connect reddit.com | |
// @downloadURL https://gist.github.com/jangxx/a0b7f9b4f9eb37d0198955caf971afef/raw/bettersyncvideo.user.js | |
// @updateURL https://gist.github.com/jangxx/a0b7f9b4f9eb37d0198955caf971afef/raw/bettersyncvideo.meta.js | |
// @require https://userscripts-mirror.org/scripts/source/107941.user.js | |
// ==/UserScript== | |
(function() { | |
var $ = jQuery; | |
var input_style = "width: 100%; height: 100%; box-sizing: border-box;"; | |
var reddit_add_form = '<form class="form-inline visible-for-op add-from-reddit-form">' + | |
'<div class="input-append" style="display: flex; flex-wrap: wrap;">' + | |
'<div style="flex-basis: 100%"><input id="subredditNameInput" type="text" placeholder="Enter subreddit name" style="' + input_style + '"></div>' + | |
'<div style="display: flex;">' + | |
'<div><select id="subredditSectionSelect" style="' + input_style + '">' + | |
'<option name="hot">hot</option>' + | |
'<option name="new">new</option>' + | |
'<option name="rising">rising</option>' + | |
'<option name="controversial">controversial</option>' + | |
'<option name="top">top</option>' + | |
'<option name="gilded">gilded</option>' + | |
'</select></div>' + | |
'<div><input id="postCountInput" type="text" placeholder="# of posts" style="' + input_style + '"></div>' + | |
'<div><input id="minUpvotesInput" type="text" placeholder="upvotes" style="' + input_style + '"></div>' + | |
'<button type="submit" class="btn btn-warning" style="flex-shrink: 0">' + | |
'<i class="icon-plus icon-white"></i></button>' + | |
'</div>' + | |
'</div></form>'; | |
$('.add-to-playlist-form').after(reddit_add_form); | |
$('.add-from-reddit-form').on('submit', function(evt) { | |
evt.preventDefault(); | |
var subreddit = $('#subredditNameInput').val(); | |
var section = $('#subredditSectionSelect').val(); | |
var post_count = $('#postCountInput').val(); | |
var min_upvotes = $('#minUpvotesInput').val(); | |
if(subreddit === "") return alert("subreddit can't be empty"); | |
if(post_count === "") post_count = 100; | |
if(min_upvotes === "") min_upvotes = 100; | |
sendWS({ | |
cmd: "say", | |
text: "Adding videos from /r/" + subreddit + "/" + section + " with " + min_upvotes + " upvotes or more (max " + post_count + ")" | |
}); | |
postMessage({ | |
type: "loadSubredditVideos", | |
subreddit: subreddit, | |
post_count: post_count, | |
min_upvotes: min_upvotes, | |
section: section | |
}); | |
}); | |
//overwrite addToPlaylist method so that the playlist part can be stripped off urls | |
unsafeWindow.addToPlaylist = function() { | |
var url = $('#playlistAddInput').val(); | |
if(url === "") return; | |
$('#playlistAddInput').val(""); | |
if(GM_SuperValue.get('remove_list_query') == "yes" || GM_SuperValue.get('remove_list_query') === undefined) { | |
if(url.match(/v=([0-9a-zA-Z]|-|_)+/) !== null) { | |
url = url.replace(/&list=([0-9a-zA-Z]|-|_)+/, ''); | |
url = url.replace(/\?list=([0-9a-zA-Z]|-|_)+/, '?'); | |
} | |
} | |
sendWS({ | |
cmd: "PlaylistPush", | |
text: url | |
}); | |
}; | |
$('#config-table tbody').append(generateOptionsCheckbox("Parse room as subreddit", "Automatically use the room id as a subreddit and begin fetching", "parse_room_as_subreddit")); | |
$('#config-table tbody').append(generateOptionsCheckbox("Strip playlist from video links", "Prevent the accidental adding of playlists, when only a single video was supposed to be added", "remove_list_query")); | |
if(GM_SuperValue.get("parse_room_as_subreddit") == "yes" || GM_SuperValue.get("parse_room_as_subreddit") === undefined) { | |
waitForConnection(parseRoomAsSubreddit); | |
} | |
window.addEventListener("message", function(evt) { | |
var message; | |
try { | |
message = JSON.parse(evt.data); | |
} catch(e) {} | |
if(!message) return; | |
if(message.type === undefined) return; | |
switch (message.type) { | |
case "loadSubredditVideos": | |
loadSubredditVideos(message); | |
break; | |
case "addVideos": | |
addVideos(message); | |
break; | |
} | |
}, false); | |
function waitForConnection(callback) { | |
setTimeout((function(callback) {return function() { | |
if(connectWS() == 0) { | |
callback.call(window); | |
} else { | |
waitForConnection(callback); | |
} | |
};})(callback), 500); | |
} | |
function parseRoomAsSubreddit() { | |
if(myPriv() != 2) return; | |
var subreddit = window.location.pathname.substring(3); | |
sendWS({ | |
cmd: "say", | |
text: "Trying to add videos from /r/" + subreddit + "/hot with 300 upvotes or more (max 100)" | |
}); | |
postMessage({ | |
type: "loadSubredditVideos", | |
subreddit: subreddit, | |
post_count: 100, | |
min_upvotes: 300, | |
section: "hot" | |
}); | |
} | |
function postMessage(messageObj) { | |
try { | |
var message = JSON.stringify(messageObj); | |
window.postMessage(message, "*"); | |
} catch(e) { | |
alert("Error while sending message"); | |
} | |
} | |
function addVideos(message) { | |
var entries = message.data; | |
var count = message.post_count; | |
for(var i = 0; i < count; i++) { | |
var entry = entries[i].data; | |
if((entry.domain == "youtube.com" || entry.domain == "youtu.be") && entry.score >= message.min_upvotes) { | |
//console.log("Add video:", entry.url, "(" + entry.score + ")"); | |
sendWS({ | |
cmd: "PlaylistPush", | |
text: entry.url | |
}); | |
sendWS({ | |
cmd: "say", | |
text: "Adding video '" + entry.title + "' (" + entry.score + ") from /r/" + entry.subreddit | |
}); | |
} | |
} | |
} | |
function loadSubredditVideos(message) { | |
var subreddit = message.subreddit; | |
var section = message.section; | |
var post_count = message.post_count; | |
var min_upvotes = message.min_upvotes; | |
requestMoreVideos(subreddit + "/" + section, post_count, min_upvotes); | |
} | |
function requestMoreVideos(subreddit, count, min_upvotes, videos, after) { | |
if(videos === undefined) videos = []; | |
after = (after === undefined) ? "" : "?after=" + after; | |
GM_xmlhttpRequest({ | |
url: "https://www.reddit.com/r/" + subreddit + ".json" + after, | |
method: "GET", | |
headers: { | |
"User-Agent": "SyncVideo Loader" | |
}, | |
ignoreCache: true, | |
responseType: "json", | |
onload: function(resp) { | |
if(resp.status != 200) return; | |
videos = videos.concat(resp.response.data.children); | |
if(videos.length >= count) { | |
postMessage({type: "addVideos", data: videos, min_upvotes: min_upvotes, post_count: count}); | |
} else { | |
requestMoreVideos(subreddit, count, min_upvotes, videos, resp.response.data.after); | |
} | |
} | |
}); | |
} | |
function generateOptionsCheckbox(title, helptext, key) { | |
var html = '<tr>' + | |
'<td><div class="inline vmid small">' + title + '</div></td>' + | |
'<td class"switch-td"></td>' + | |
'</tr>'; | |
var switch_elem = $('<div data-dynamic="show-tooltip" data-text="' + helptext + '" data-on="warning" class="switch switch-mini switch-enabled-for-owner vmid inline"><input type="checkbox"/></div>'); | |
var value = GM_SuperValue.get(key); | |
value = (value === undefined) ? "yes" : value; | |
switch_elem.bootstrapSwitch(); | |
switch_elem.on('switch-change', (function(key) {return function(evt, data) { | |
var value = (data.value) ? "yes" : "no"; | |
GM_SuperValue.set(key, value); | |
};})(key)); | |
if(value == "yes") { | |
switch_elem.bootstrapSwitch("setState", true); | |
} | |
var elem = $(html); | |
elem.children().eq(1).append(switch_elem); | |
return elem; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment