Add Suggestions to OAPEN Library

Adds suggestions to the sidebar on https://library.oapen.org when viewing a publication.

As of 10.05.2023. See апошняя версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Add Suggestions to OAPEN Library
// @namespace   Violentmonkey Scripts
// @match       *://library.oapen.org/handle/*
// @grant       none
// @version     1.0
// @author      OAPEN Suggestion Service
// @description Adds suggestions to the sidebar on https://library.oapen.org when viewing a publication.
// ==/UserScript==
const FETCH_URL_HOST = "https://oss.ebookfoundation.org";

async function mountSuggestions() {
  let sidebar = document.querySelector("#aspect_artifactbrowser_ItemViewer_div_item-view > div > div.row > div.col-sm-4");
  let suggestionsElement = document.createElement("div");
  suggestionsElement.className = "item-page-field-wrapper list-group";
  let heading = document.createElement("h5");
  heading.innerText = "Suggestions"
  suggestionsElement.appendChild(heading);

  let handle = window.location.pathname.split('/').slice(-2).join("/");
  const response = await fetch(FETCH_URL_HOST + "/api/" + handle);
  const resp = await response.json();

  if (!resp.items || !resp?.items?.suggestions) {
    console.error("No items in response!");
    return;
  }

  const { suggestions } = resp.items;

  if (suggestions.length === 0 || suggestions === []) {
    console.error("No suggestions in response!");
    return;
  }

  // create suggestions list element
  const suggestionsListElement = document.createElement("ul");
  suggestionsListElement.className = "suggestions-list";

  // create suggestions list items
  const suggestionsListItems = suggestions.slice(0, 4).map((suggestion) => {
    const listItem = document.createElement("li");
    listItem.className = "list-group-item ds-option";
    listItem.style.textAlign = "center";
    // make it a link
    const link = document.createElement("a");
    link.className = "suggestions-list-item-link";
    link.href = "https://library.oapen.org/handle/" + suggestion.suggestion;
    link.target = "_blank";
    link.innerText = suggestion.suggestion_name;
    // add the thumbail
    const thumbnail = document.createElement("img");
    const thumbnailDiv = document.createElement("div");
    thumbnailDiv.className = "thumbnail";
    thumbnail.className = "img-thumbnail";
    thumbnail.style.margin = "0 auto";
    thumbnail.src = suggestion.suggestion_thumbnail;
    // append it all together
    thumbnailDiv.appendChild(thumbnail);
    listItem.appendChild(thumbnailDiv);
    listItem.appendChild(link);
    return listItem;
  });

  // append suggestions list items to list
  suggestionsListItems.forEach((item) =>
    suggestionsListElement.appendChild(item)
  );

  // append list to suggestions element
  suggestionsElement.appendChild(suggestionsListElement);

  // append suggestions to sidebar
  sidebar.appendChild(suggestionsElement);
}

window.onload = function () {
  mountSuggestions();
};