Last active
November 15, 2017 17:54
-
-
Save ElfhirDev/751ebfc75235d7712adcf8924f0b35b6 to your computer and use it in GitHub Desktop.
Add toolbar functionalities : go to the top/bottom of the page, refresh,
This file contains hidden or 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 avenoel-toolkit | |
// @namespace avnt | |
// @description toolbar for scroll to top, to bottom, refresh, youtube-player from cdv | |
// @include https://avenoel.org/* | |
// @version 1.03 | |
// @grant none | |
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js | |
// @run-at document-end | |
// ==/UserScript== | |
"use strict"; | |
$(document).ready(function () { | |
var avnt = { | |
player: {}, | |
messages : [], | |
addToolBar : function () { | |
if ($('#backtotop').length == 0) { | |
let gototop_l = "<li class='gototop'><a href='#top'><span class='glyphicon glyphicon-arrow-up'></span></a></li>"; | |
let gotobottom_l = "<li class='gotobottom'><a href='#bottom'><span class='glyphicon glyphicon-arrow-down'></span></a></li>"; | |
let gototop_sm = "<li class='gototop'><a href='#top'><span class='glyphicon glyphicon-arrow-up'></span></a></li>"; | |
let gotobottom_sm = "<li class='gotobottom'><a href='#bottom'><span class='glyphicon glyphicon-arrow-down'></span></a></li>"; | |
$(".navbar-user li:first-of-type").after(gototop_l); | |
$(".navbar-user li:first-of-type").after(gotobottom_l); | |
$(".bottombar li:first-of-type").after(gototop_sm); | |
$(".bottombar li:first-of-type").after(gotobottom_sm); | |
} | |
}, | |
addRefreshButton : function() { | |
let button_l = "<a class=\'btn btn-primary pull-right refresh-page\' href=\'#refresh\'>Actualiser</a>" | |
var path = window.location.pathname; | |
if (path.startsWith("/topic")) { | |
$(".pagination + a").after(button_l); | |
} | |
else if (path.startsWith("/forum")) { | |
$(".pagination:last-of-type").after("<div class='row'>"+button_l+"</div>"); | |
} | |
}, | |
createEventListener : function () { | |
var self = this; | |
$(".avnt .gototop").on('click touchstart', function (e) { | |
e.stopPropagation(); | |
document.body.scrollTop = document.documentElement.scrollTop = 0; | |
}); | |
$(".avnt .gotobottom").on('click touchstart', function (e) { | |
e.stopPropagation(); | |
window.scrollTo(0, document.body.scrollHeight); | |
}); | |
$(".avnt .refresh-page").on('click touchstart', function (e) { | |
e.stopPropagation(); | |
document.location.reload(true); | |
}); | |
$(window).on("scroll", function(e) { | |
var y = $(this).scrollTop(); | |
if (y > 800) { | |
$('#avnt-player').css({"position": "fixed", "top":"15%"}); | |
} else { | |
$('#avnt-player').css({"position": "static"}); | |
} | |
}); | |
}, | |
createPlayerEventListener : function() { | |
var self = this; | |
$(".avnt-cdv .cdv-music-play").on("click touchstart", function(e) { | |
$("#avnt-player").remove(); | |
$(".avnt .container-content").append("<div class='col-md-3 col-sm-12 col-xs-12 pull-right hidden-sm hidden-xs'><iframe id='avnt-player' width='260' height='120' src='https://www.youtube.com/embed/"+$(this).data('music')+"?enablejsapi=1&version=3&autoplay=1&origin="+window.location+"' frameborder='1'></iframe></div>"); | |
var y = $(window).scrollTop(); | |
if (y > 800) { | |
$('#avnt-player').css({"position": "fixed", "top":"15%"}); | |
} | |
$(".avnt-cdv").data("state", "play"); | |
$(".avnt-cdv .cdv-music-play").css("color", "initial"); | |
$(this).css("color", "#459359"); | |
}); | |
$(".avnt .cdv-music-stop").on("click touchstart", function(e) { | |
e.stopPropagation(); | |
$("#avnt-player").remove(); | |
$(".avnt-cdv").data("state", "stop"); | |
$(".avnt-cdv .cdv-music-play").css("color", "initial"); | |
}); | |
}, | |
getMessages : function() { | |
var self = this; | |
$('.topic-messages > article').each(function(i) { | |
let message_id = $(this).attr("id"); | |
var message_selector = $(this); | |
$.get("https://avenoel.org/api/v1/messages/"+message_id, function(data) { | |
}).done(function(data) { | |
self.getUserDataFromMessage(data.data, message_selector); | |
}).fail(function() { | |
console.warn("[AVNT] : failed to get messages"); | |
}); | |
}); | |
}, | |
getUserDataFromMessage : function(message, message_selector) { | |
var self = this; | |
$.get("https://avenoel.org/api/v1/user/"+message.user_id, function(data) { | |
}).done(function(data) { | |
self.messages.push({"user_id": message.user_id, "music": data.data.music}); | |
if (data.data.music !== "") { | |
$(message_selector).find(".message-infos").after("<div class='avnt-cdv' data-state='stop'><a href='#play' class='cdv-music-play' data-music='"+data.data.music+"'><span class='glyphicon glyphicon-play'></span></a>"+ | |
"<a href='#stop' class='cdv-music-stop' ><span class='glyphicon glyphicon-stop'></span></a>"); | |
self.createPlayerEventListener(); | |
} | |
}).fail(function() { | |
console.warn("[AVNT] : failed to get user"); | |
}); | |
}, | |
init : function() { | |
$("body").addClass("avnt"); | |
this.addToolBar(); | |
this.addRefreshButton(); | |
this.getMessages(); | |
this.createEventListener(); | |
} | |
}; | |
avnt.init(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment