jnaskali / IMDb Trakt Link

// ==UserScript==
// @name        IMDb Trakt Link
// @author      jnaskali
// @copyright   2021, Juhani Naskali (www.naskali.fi)
// @license     MIT
// @version			1.3
// @namespace   https://www.naskali.fi
// @updateURL   https://openuserjs.org/meta/jnaskali/IMDb_Trakt_Link.meta.js
// @downloadURL https://openuserjs.org/install/jnaskali/IMDb_Trakt_Link.user.js
//
// @include			https://*.imdb.com/title/*
// @grant			  none
// @run-at      document-idle
//
// @description Add link to Trakt for IMDb title pages
// ==/UserScript==

/* jshint esversion: 6 */

var style = document.createElement('style');
style.innerHTML = `
  .trakt-link {
    font-size: 12px;
    font-weight: bold;
    text-align: right;
    letter-spacing: 2px; 
    color: rgba(255,255,255,0.7);
    text-decoration: none;
  }
  .trakt-link > img {
    width: 32px;
    height: 32px;
  }


`;
document.head.appendChild(style);

function addTrakt () {
  var link = document.createElement('a');
  link.href = 'https://trakt.tv/search/imdb?q=tt' + getIMDBid();
  link.innerHTML = 'TRAKT<br/><img src="https://walter.trakt.tv/hotlink-ok/public/favicon.ico" alt="Search Trakt.tv"/>';
  link.className = 'trakt-link';
  document.querySelector('div[class*="rating"][data-testid*="aggregate"]').parentNode.appendChild(link);
}
  
function getIMDBid () {
	var regex = /\/title\/tt(\d*)\//;
	return regex.exec(document.location)[1];
}

addTrakt();