Last active
January 22, 2022 18:04
-
-
Save kosorin/7773d66b2a390c87012297da2daf2e46 to your computer and use it in GitHub Desktop.
Trakt to ČSFD
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
// ==UserScript== | |
// @name Trakt to ČSFD | |
// @version 1.2 | |
// @description Search current title on ČSFD | |
// @match *://trakt.tv/* | |
// @author David Kosorin | |
// @source https://gist.github.com/kosorin/7773d66b2a390c87012297da2daf2e46 | |
// @namespace https://trakt.tv/ | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const linkContainerSelector = "ul.external > li"; | |
const getLinkContainer = () => document.querySelector(linkContainerSelector); | |
const addLink = (linkContainer, csfd) => { | |
if (!csfd) { | |
return; | |
} | |
const csfdNode = document.createElement("a"); | |
csfdNode.innerText = " ČSFD"; | |
csfdNode.target = "_blank"; | |
csfdNode.href = csfd.url; | |
const searchNode = document.createElement("i"); | |
searchNode.className = "fa fa-search"; | |
csfdNode.insertBefore(searchNode, csfdNode.firstChild); | |
getLinkContainer().appendChild(csfdNode); | |
} | |
const linkContainerCallback = (linkContainer) => { | |
console.log("ČSFD callback"); | |
let titleNode = document.querySelector('meta[property="og:title"]'); | |
if (!titleNode) { | |
return; | |
} | |
let csfd = { | |
url: "https://www.csfd.cz/hledat/?q=" + encodeURIComponent(titleNode.content) | |
}; | |
console.log("ČSFD search:", csfd); | |
addLink(linkContainer, csfd); | |
}; | |
const linkContainerObserverCallback = () => { | |
let linkContainer = getLinkContainer(); | |
if (linkContainer == null) { | |
return; | |
} | |
const observedNodeAttributeName = "observed-trakt-csfd"; | |
if (linkContainer.getAttribute(observedNodeAttributeName) != null) { | |
return; | |
} | |
linkContainer.setAttribute(observedNodeAttributeName, true); | |
linkContainerCallback(linkContainer); | |
}; | |
const observerCallback = () => { | |
linkContainerObserverCallback(); | |
}; | |
const observer = new MutationObserver((mutations, observer) => observerCallback()); | |
observer.observe(document.documentElement, { childList: true, subtree: true }); | |
observerCallback(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment