Skip to content

Commit

Permalink
Add initial Trakt.tv support
Browse files Browse the repository at this point in the history
Ref #6
  • Loading branch information
SpaceK33z committed Dec 17, 2016
1 parent 3970e2b commit fdaca22
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
"matches": ["*://*.imdb.com/title/tt*"],
"js": ["utils.js", "imdb.js"],
"css": ["imdb.css"]
},
{
"matches": ["https://trakt.tv/*"],
"js": ["history-hack.js", "utils.js", "trakt.js"],
"css": ["trakt.css"]
}
],
"background": {
Expand Down
38 changes: 38 additions & 0 deletions src/trakt.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.web-to-plex-button--couchpotato {
border-color: #f45a26!important;
background: #f45a26!important;
color: #fff!important;
}

.web-to-plex-button--couchpotato:hover {
background: #f67e56!important;
}

.web-to-plex-button--found {
background: #e5a00d!important;
border-color: #e5a00d!important;
color: #fff!important;
}

.web-to-plex-button--found:hover {
background: #f9be03!important;
}

.web-to-plex-notification {
display: block;
position: fixed;
top: 80px;
left: 50%;
margin-left: -175px;
width: 350px;
background: #21262e;
color: #fff;
font-size: 20px;
padding: 10px;
border-radius: 4px;
z-index: 999000;
}

.web-to-plex-warning {
background: #fd6a00;
}
69 changes: 69 additions & 0 deletions src/trakt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
function isMoviePage() {
const path = window.location.pathname;
if (!path.startsWith('/movies/')) {
return false;
}
// TODO: e.g. /movies/trending is not really a movie page...
return true;
}

function isMoviePageReady() {
return true;
}

function getImdbId() {
const $link = document.querySelector('ul.external [href^="http://www.imdb.com/title/tt"]');
if ($link) {
return $link.href.replace('http://www.imdb.com/title/', '');
}
return null;
}

function init() {
if(isMoviePage()) {
wait(() => document.querySelector('#info-wrapper ul.external'), () => {
initPlexThingy();
});
}
}

function renderPlexButton() {
const $actions = document.querySelector('ul.external li:first-child');
if (!$actions) {
console.log('Could not add Plex button.');
return;
}
const el = document.createElement('a');
el.classList.add('web-to-plex-button');
$actions.insertBefore(el, $actions.childNodes[0]);
return el;
}

function initPlexThingy() {
const $button = renderPlexButton();
if (!$button) {
return;
}
const $title = document.querySelector('.btn-checkin');
const $year = document.querySelector('.summary .mobile-title .year');
if (!$title || !$year) {
modifyPlexButton($button, 'error', 'Could not extract title or year');
return;
}
const title = $title.dataset.topTitle;
const year = parseInt($year.textContent.trim());
const imdbId = getImdbId();

handlePlex(config, { title, year, button: $button, imdbId });
}

let config;
getOptions().then((options) => {
config = options;
window.addEventListener('popstate', init);
window.addEventListener('pushstate-changed', init);
init();
}, () => {
showNotification('warning', 'Not all options for the Movieo to Plex extension are filled in.');
});

7 changes: 7 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
function wait(check, then) {
if (check()) {
then();
} else {
setTimeout(() => wait(check, then), 50);
}
}

function doPlexRequest(config, options) {
// TODO: it is possible that there are multiple movie sections in Plex, so optimally we'd loop through all of them.
Expand Down

0 comments on commit fdaca22

Please sign in to comment.