Add Suggestions to OAPEN Library

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

10.05.2023 itibariyledir. En son verisyonu görün.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

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

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

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.

(Zateb bir user-style yöneticim var, yükleyeyim!)

// ==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();
};