Last active
August 29, 2015 14:02
-
-
Save outring/a0d35a544f1056bcf1b6 to your computer and use it in GitHub Desktop.
Watch.is links bookmarklet
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
(function() { | |
if (location.host !== "watch.is") { | |
alert("Для начала надо зайти на Watch.is, например."); | |
return; | |
} | |
var errorHeading = "Что-то пошло не так."; | |
var apiTypes = [ | |
{ id: "video", name: "Просто видео" }, | |
{ id: "hdvideo", name: "Просто видео в HD" }, | |
{ id: "hls", name: "HLS-видео", localizable: true }, | |
{ id: "hdhls", name: "HLS-видео в HD", localizable: true } | |
]; | |
var windowId = "linksBookmarklet"; | |
var windowStyle = { | |
position: "absolute", | |
top: "300px", | |
left: 0, | |
right: 0, | |
width: "175px", | |
margin: "auto", | |
padding: "10px 20px", | |
backgroundColor: "#666666", | |
borderRadius: "15px" | |
}; | |
var rowStyle = { | |
margin: "5px 0" | |
}; | |
var videoId = (location.pathname.match(/^\/(\d+)/) || [])[1]; | |
if (!videoId) { | |
alert("Надо зайти на страницу с видео и тогда всё получится."); | |
return; | |
} | |
$("#" + windowId).remove(); | |
$.ajax("/api/watch/" + videoId, { dataType: "xml" }) | |
.done(function(data){ | |
var $data = $(data); | |
if (!$data.find("id").text()) { | |
alert(errorHeading + " Такого видео как будто бы нет."); | |
return; | |
} | |
var links = []; | |
$data.find(apiTypes.map(function(type) { return type.id }).join(",")) | |
.each(function(_, source) { | |
var type = apiTypes.filter(function(type) { return type.id === source.tagName })[0]; | |
var url = source.textContent.trim(); | |
links.push({ | |
typeId: type.id, | |
name: type.name, | |
url: url, | |
localizedUrl: type.localizable ? getLocalizedSource(url) : null | |
}); | |
}) | |
.get(); | |
if (links.length === 0) { | |
alert(errorHeading + " Ссылок на видео не нашлось."); | |
return; | |
} | |
var $window = $("<div />", { id: windowId, css: windowStyle }).appendTo("body"); | |
$.each(links, function (_, link) { | |
var $row = $("<div />", { css: rowStyle }); | |
$row.append($("<a />", { href: link.url }).text(link.name)); | |
if (link.localizedUrl) | |
$row.append(" ").append($("<a />", { href: link.localizedUrl }).text("Оригинал")); | |
$row.appendTo($window); | |
}); | |
}) | |
.fail(function(response, status, error) { | |
alert(errorHeading + " Ошибка вот такая: " + error); | |
}); | |
function getLocalizedSource(source) { | |
return source.replace("?", "?audioIndex=1&"); | |
} | |
})(); |
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
javascript:(function(){function g(a){return a.replace("?","?audioIndex=1&")}if("watch.is"!==location.host)return alert("Для начала надо зайти на Watch.is, например."),void 0;var a="Что-то пошло не так.",b=[{id:"video",name:"Просто видео"},{id:"hdvideo",name:"Просто видео в HD"},{id:"hls",name:"HLS-видео",localizable:!0},{id:"hdhls",name:"HLS-видео в HD",localizable:!0}],c="linksBookmarklet",d={position:"absolute",top:"300px",left:0,right:0,width:"175px",margin:"auto",padding:"10px 20px",backgroundColor:"#666666",borderRadius:"15px"},e={margin:"5px 0"},f=(location.pathname.match(/^\/(\d+)/)||[])[1];return f?($("#"+c).remove(),$.ajax("/api/watch/"+f,{dataType:"xml"}).done(function(f){var h=$(f);if(!h.find("id").text())return alert(a+" Такого видео как будто бы нет."),void 0;var i=[];if(h.find(b.map(function(a){return a.id}).join(",")).each(function(a,c){var d=b.filter(function(a){return a.id===c.tagName})[0],e=c.textContent.trim();i.push({typeId:d.id,name:d.name,url:e,localizedUrl:d.localizable?g(e):null})}).get(),0===i.length)return alert(a+" Ссылок на видео не нашлось."),void 0;var j=$("<div />",{id:c,css:d}).appendTo("body");$.each(i,function(a,b){var c=$("<div />",{css:e});c.append($("<a />",{href:b.url}).text(b.name)),b.localizedUrl&&c.append(" ").append($("<a />",{href:b.localizedUrl}).text("Оригинал")),c.appendTo(j)})}).fail(function(b,c,d){alert(a+" Ошибка вот такая: "+d)}),void 0):(alert("Надо зайти на страницу с видео и тогда всё получится."),void 0)})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment