Greasy Fork

Criticker - Add Letterboxd Search Button

Adds a button to search the movie on Letterboxd

// ==UserScript==
// @name         Criticker - Add Letterboxd Search Button
// @namespace    https://www.criticker.com/
// @version      1.0
// @description  Adds a button to search the movie on Letterboxd
// @author       n00bCod3r (https://github.com/n00bCod3rr)
// @match        https://www.criticker.com/films*
// @grant        none
// ==/UserScript==

(function() {

  'use strict';
  function formatTitle(title) {
    title.trim();
    title = title.replaceAll('#','%23')
    title = title.replaceAll('+','%2B')
    title = title.replaceAll('&','%26')
    title = title.replaceAll('\'','%27')
    title = title.replaceAll('?', '%3F')
    title = title.replaceAll(' ', '+')
    return title;
  }

  //lbx logo
  const logoURL = 'https://a.ltrbxd.com/logos/letterboxd-decal-dots-pos-rgb-500px.png';
  //all li elements
  const titlesList = document.querySelectorAll('main>ul.fl_titlelist li');
  titlesList.forEach(el => {
    const div = el.querySelector('.fl_name'); //cotnains <a> with movie info
    console.log(div);
    const title = formatTitle(div.querySelector('a').title);
    //search link for the movie
    const lbxURL = `https://letterboxd.com/search/films/${title}/`;
    //<a> containing the lbx logo image
    const imgAnchor = document.createElement('a');
    imgAnchor.href = lbxURL;
    imgAnchor.target = "blank";
    imgAnchor.rel = "noopener noreferrer";
    //lbx logo image
    const img = document.createElement('img');
    img.src = logoURL;
    img.width = '20';
    imgAnchor.appendChild(img);
    div.prepend(imgAnchor);
  })

})();