Add Suggestions to OAPEN Library

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

当前为 2023-05-10 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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