Skip to content

Instantly share code, notes, and snippets.

@toyg
Last active June 25, 2018 14:39
Show Gist options
  • Save toyg/c00c9fcc0a7c636ed2138c7d2b690f96 to your computer and use it in GitHub Desktop.
Save toyg/c00c9fcc0a7c636ed2138c7d2b690f96 to your computer and use it in GitHub Desktop.
Synopsifier.user.js
// ==UserScript==
// @name Synopsifier
// @namespace http://pythonaro.com/
// @version 1.0
// @description add movie details when browsing directories.
// @author toyg
// @match http://mc1.dl3enter.in/*
// @grant GM_xmlhttpRequest
// @connect themoviedb.org
// ==/UserScript==
/** USAGE NOTES:
1. get an api key from Themoviedb.org
2. add it to the script replacing the placeholder text
3. edit the @match clause above to match your directories
4. save the file with extension .user.js and open it in browser to install
**/
var APIKEY = "YOUR THEMOVIEDB API KEY HERE";
function make_img_url(endpath){
return "https://image.tmdb.org/t/p/w92" + endpath;
}
function build_details_box(detailObj, previousNode){
var descNode = document.createElement("p");
if(detailObj.poster_path){
var imgNode = document.createElement("img");
imgNode.setAttribute("src", make_img_url(detailObj.poster_path));
descNode.appendChild(imgNode);
}
descNode.setAttribute('class','desc');
descNode.appendChild(document.createTextNode(detailObj.overview));
previousNode.parentNode.insertBefore(descNode, previousNode.nextSibling.nextSibling);
}
function build_multichoice_box(choices, previousNode){
var descNode = document.createElement("p");
descNode.setAttribute('class','desc');
descNode.appendChild(document.createTextNode("Might be one of these:"));
var ulNode = document.createElement("ul");
descNode.appendChild(ulNode);
for(var choice in choices){
var choiceLi = document.createElement("li");
choiceLi.appendChild(document.createTextNode(choices[choice].title));
choiceLi.appendChild(document.createElement("br"));
choiceLi.appendChild(document.createTextNode(choices[choice].overview));
ulNode.appendChild(choiceLi);
}
previousNode.parentNode.insertBefore(descNode, previousNode.nextSibling.nextSibling);
}
function get_details(title, year, nodeRef){
GM_xmlhttpRequest({
method: "GET",
url: "https://api.themoviedb.org/3/search/movie?query="+encodeURIComponent(title)+"&year="+year+"&api_key=" + APIKEY,
onload: function(response) {
var details = JSON.parse(response.responseText);
switch(details.total_results){
case 0:
return false;
case 1:
build_details_box(details.results[0], nodeRef);
break;
default:
var multiChoice = [];
for(var result in details.results){
var resultObj = details.results[result];
if(resultObj.title == title){
build_details_box(resultObj, nodeRef);
multiChoice = [];
return false;
} else {
multiChoice.push(resultObj);
}
}
if(multiChoice.length !== 0){
build_multichoice_box(multiChoice, nodeRef);
}
}
}
});
}
(function() {
'use strict';
var h1Nodes = document.evaluate('/html/body/h1', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var indexTitle = h1Nodes.snapshotItem(0).firstChild.nodeValue.substring(9);
if(indexTitle.match('.*(/Movie/[0-9]+)/$') || indexTitle.match('.*(/film/[0-9]+)/$')) {
var elmHead, elmStyle;
elmHead = document.getElementsByTagName('head')[0];
elmStyle = document.createElement('style');
elmStyle.type = 'text/css';
elmHead.appendChild(elmStyle);
var cssRule = 'p.desc { margin:0; white-space:normal; font-family: Verdana, Helvetica, Arial, sans-serif; ';
cssRule += 'overflow: hidden; font-size: small; display: block; width: 500px; padding:4px; clear: both;}\n';
cssRule += 'a { font-weight: bold; font-family: Arial, Helvetica, sans-serif; color:#136CB2; ';
cssRule += 'padding: 3px; margin:0;margin-top: 4px; clear:right;}\n';
cssRule += 'img {float: right; margin-left: 4px;}\n';
elmStyle.innerHTML = cssRule;
var year = indexTitle.substring(indexTitle.length - 5, indexTitle.length - 1);
var aNodes = document.evaluate('//a[@href!="../"]', document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var i = aNodes.snapshotLength - 1; i >= 0; i--) {
var aNode = aNodes.snapshotItem(i);
var aTitle = aNode.firstChild.nodeValue;
aTitle = aTitle.substring(0,aTitle.length-1);
var targetSec = Math.floor(i / 4);
window.setTimeout(get_details, targetSec*1100, aTitle, year, aNode);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment