Skip to content

Instantly share code, notes, and snippets.

@wirx6
Last active July 26, 2016 15:58
Show Gist options
  • Save wirx6/8096e75aa69d1e542ab9efcadd294c43 to your computer and use it in GitHub Desktop.
Save wirx6/8096e75aa69d1e542ab9efcadd294c43 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Discord Media Utility
// @namespace wirx6
// @author wirx6
// @version 0.3
// @match https://discordapp.com/channels/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var queue = [];
function populateQueue(){
// get nodelist: convert to array so we can "pop" it
queue = Array.prototype.slice.call(document.querySelectorAll('.message'));
}
function embedVideo(latestElement, src){
console.log('Embeding: '+src);
// new html5 video player
var videoPlayer = document.createElement('video');
videoPlayer.src = src;
// enable controls: give class so Discord can style it and we can check it later
videoPlayer.setAttribute('controls', 'controls');
videoPlayer.setAttribute('class', 'html5-video-player');
// style the player so it doesn't get 2hueg
videoPlayer.setAttribute('style', 'max-width: 400px; max-height: 300px; border-radius: 5px;');
// attach player to message
latestElement.appendChild(videoPlayer);
}
function checkToConvertVideoToPlayer() {
// Populate queue if empty
if (queue.length === 0) {
populateQueue();
} else {
var latestElement = queue.pop();
// video player already in message: stop
if (!latestElement.querySelector('.html5-video-player')){
// attachment found:
if (latestElement.querySelector('.attachment')) {
var src = latestElement.querySelector('.attachment').querySelector('a').href;
// src is video file:
if (src.endsWith('.mp4') || src.endsWith('.webm')) {
embedVideo(latestElement, src);
}
}
if (latestElement.querySelector('.markup').querySelector('a')) {
var src = latestElement.querySelector('.markup').querySelector('a').href;
// src is video file:
if (src.endsWith('.mp4') || src.endsWith('.webm')) {
embedVideo(latestElement, src);
}
}
}
}
requestAnimationFrame(checkToConvertVideoToPlayer);
}
function addGraphicSearch(latestElement, src){
latestElement.appendChild(document.createElement('br'));
var gSearch = document.createElement('a');
gSearch.href = 'http://www.google.com/searchbyimage?image_url='+src;
gSearch.innerHTML = '[google]';
gSearch.setAttribute('class', 'search');
gSearch.setAttribute('target', '_blank');
latestElement.appendChild(gSearch);
var eSearch = document.createElement('a');
eSearch.href = 'http://regex.info/exif.cgi?url='+src;
eSearch.innerHTML = '[exif]';
eSearch.setAttribute('class', 'search');
eSearch.setAttribute('target', '_blank');
latestElement.appendChild(eSearch);
var wSearch = document.createElement('a');
wSearch.href = 'https://whatanime.ga/?url='+src;
wSearch.innerHTML = '[whatanime]';
wSearch.setAttribute('class', 'search');
wSearch.setAttribute('target', '_blank');
latestElement.appendChild(wSearch);
var tSearch = document.createElement('a');
tSearch.href = 'https://tineye.com/search?url='+src;
tSearch.innerHTML = '[tineye]';
tSearch.setAttribute('class', 'search');
tSearch.setAttribute('target', '_blank');
latestElement.appendChild(tSearch);
var iSearch = document.createElement('a');
iSearch.href = 'https://iqdb.org/?url='+src;
iSearch.innerHTML = '[iqdb]';
iSearch.setAttribute('class', 'search');
iSearch.setAttribute('target', '_blank');
latestElement.appendChild(iSearch);
}
setInterval(() => {
var iList = document.querySelectorAll('a[href$=".jpg"], a[href$=".png"], a[href$=".gif"]');
for (var i = 1; i < iList.length; i++) {
if((iList[i].className != 'search') && (iList[i-1].className != 'search')){
addGraphicSearch(iList[i-1], iList[i-1].href);
}
}
}, 2000);
requestAnimationFrame(checkToConvertVideoToPlayer);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment