Skip to content

Простой скриптик для создания кнопки IMDB рейтинга на странице

Скрипт дает возможность создавать вот такую кнопку    одним тегом.

Скрипт можно подключать в любом месте страницы.

Пример кода вставки кнопки: <a id="thirdparty-imdb-rating-tt0245429" class="thirdparty-imdb-rating">  </a>

Дополнительный источник: https://gist.github.com/bogdan-nazar/86b241e4786b8d90cf40

/**
* Simple snippet to embed IMDB rating button to your web-page
* Author: Bogdan Nazar <me@bogdan-nazar.ru>
*
* USAGE: <div class="thirdparty-imdb-rating" id="thirdparty-imdb-rating-ttXXXXXXX"></div>
*        where "ttXXXXXXX" is the uniq IMDB title identifier
*
* IMPORTANT: Before using this snippet you must replace
*   var user = "urXXXXXXXX"
* with your real IMDB user id.
*  
*/
(function(){
  var user = "urXXXXXXXX";
  var $ = {d: document, h: document.getElementsByTagName("HEAD")[0], w: window},
    css = [...

/**
* Simple snippet to embed IMDB rating button to your web-page
* Author: Bogdan Nazar <me@bogdan-nazar.ru>
*
* USAGE: <div class="thirdparty-imdb-rating" id="thirdparty-imdb-rating-ttXXXXXXX"></div>
*        where "ttXXXXXXX" is the uniq IMDB title identifier
*
* IMPORTANT: Before using this snippet you must replace
* 	var user = "urXXXXXXXX"
* with your real IMDB user id.
*  
*/
(function(){
	var user = "urXXXXXXXX";
	var $ = {d: document, h: document.getElementsByTagName("HEAD")[0], w: window},
		css = [
		".imdbRatingPlugin{-moz-border-radius:5px;-ms-border-radius:5px;-o-border-radius:5px;-webkit-border-radius:5px;border-radius:5px;color:#000;display:inline-block;font-family:Arial,Helvetica,sans-serif;font-weight:bold;text-decoration:none;margin:5px 0}",
		".imdbRatingPlugin a{text-decoration:none}",
		".imdbRatingPlugin img{border:0;vertical-align:text-bottom}",
		".imdbRatingPlugin span.rating{display:inline-block}",
		".imdbRatingPlugin span.ofTen{color:#666;font-size:9px}",
		".imdbRatingPlugin img.star{margin-left:3px}",
		".imdbRatingPlugin span.votes{padding-left:2px;font-size:13px;color:#666;font-weight:normal;margin-left:3px}",
		".imdbRatingStyle1{background-color:#fff;border:1px solid #666;font-size:18px;padding:2px 2px 1px}",
		".imdbRatingStyle2{background-color:#000;font-size:15px;color:#fff;padding:2px}",
		".imdbRatingStyle2 span.ofTen{color:#ccc}",
		".imdbRatingStyle2 img.star{margin-bottom:1px}",
		".imdbRatingStyle3{font-size:17px}",
		".imdbRatingStyle4{font-size:14px}",
		".imdbRatingStyle5{font-size:13px}",
		".imdbRatingStyle5 span.votes{font-size:11px}"],
		cssDone = false,
		imdb = {
			rating: {}
		},
		items = {},//tt2632044
		urlData = "http://p.media-imdb.com/static-content/documents/v1/title/{%title%}/ratings%3Fjsonp=imdb.rating.run:imdb.api.title.ratings/data.json?u={%user%}&s=p1";
	var render = function(i) {
		var temp = items[i.resp.resource.id.split("/")[2]];
		if (!temp) return;
		if (!cssDone) {
			cssDone = true;
			var ss = $.d.createElement("STYLE");
			ss.setAttribute("type", "text/css");
			$.h.appendChild(ss);
			if (ss.sheet && (typeof ss.sheet.insertRule == "function")) {
				ss.appendChild($.d.createTextNode(""));//для safari
				var c = 0, l = css.length;
				for (; c < l; c++)
				try {
					ss.sheet.insertRule(css[c], ss.sheet.cssRules.length);
				} catch(e) {
					console.log("IMDB css-rule insertion failed [" + css[c] + "].")
				}
			} else {
				if (ss.styleSheet && ss.styleSheet.cssText) ss.styleSheet.cssText = css.join("");
			}
		}
		var el, el1, el2;
		i.dom = {};
		i.dom.wrap = $.d.createElement("SPAN");
		temp.parentNode.insertBefore(i.dom.wrap, temp);
		temp.parentNode.removeChild(temp);
		i.dom.wrap.className = "imdbRatingPlugin imdbRatingStyle1";
		el = $.d.createElement("A");
		el.href = "http://www.imdb.com" + i.resp.resource.id;
		el.target = "_blank";
		el1 = $.d.createElement("IMG");
		el1.src = "http://g-ecx.images-amazon.com/images/G/01/imdb/plugins/rating/images/imdb_46x22.png";
		el1.title = i.resp.resource.title;
		el.appendChild(el1);
		i.dom.wrap.appendChild(el);
		el = $.d.createElement("SPAN");
		el.className = "rating";
		el.innerHTML = i.resp.resource.rating;
		el1 = $.d.createElement("SPAN");
		el1.className = "ofTen";
		el1.innerHTML = "/10";
		el.appendChild(el1);
		i.dom.wrap.appendChild(el);
		el = $.d.createElement("IMG");
		el.src = "http://g-ecx.images-amazon.com/images/G/01/imdb/plugins/rating/images/imdb_star_22x21.png";
		el.className = "star";
		i.dom.wrap.appendChild(el);
	};
	var run = imdb.rating.run = function(resp) {
		var i = {
			_rendered: false,
			resp: resp
		};
		render(i);
	};
	$.w.imdb = imdb;
	var ival, itry = 0, imax = 240;
	ival = $.w.setInterval(function() {
		itry++;
		var els = $.d.querySelectorAll(".thirdparty-imdb-rating"),
			el, l = els.length, tt;
		if (!l) return;
		for (; l--;) {
			tt = els[l].id.replace("thirdparty-imdb-rating-", "");
			if (!tt) continue;
			items[tt] = els[l];
			els[l].className = "thirdparty-imdb-rating-started";
			el = $.d.createElement("SCRIPT");
			el.setAttribute("type", "text/javascript");
			el.src = urlData.replace("{%title%}", tt).replace("{%user%}", user);
			$.h.appendChild(el);
		}
		if (itry > imax) $.w.clearInteval(ival);
	}, 500);
})();