Hide RYM Ratings If Unrated

Hides RYM ratings if you haven't rated them - unless you click a button.

目前為 2020-10-10 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Hide RYM Ratings If Unrated
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  Hides RYM ratings if you haven't rated them - unless you click a button.
// @author       w_biggs (~joks)
// @match        https://rateyourmusic.com/artist/*
// @match        https://rateyourmusic.com/release/*
// @run-at       document-start
// @grant        GM_addStyle
// ==/UserScript==

const typeMeta = document.querySelector('meta[property="og:type"]');
let isRelease = false;
let isProfile = false;
if (typeMeta.getAttribute('content') === 'music.album') {
  console.log('is release!');
  isRelease = true;
} else if (typeMeta.getAttribute('content') === 'profile') {
  console.log('is profile!');
  isProfile = true;
}

const initHideStyles = document.createElement('style');
initHideStyles.id = 'initial-hide-styles';
if (isRelease) {
  initHideStyles.innerText = '.avg_rating, .avg_rating_friends, .track_rating { opacity: 0 !important; }';
} else if (isProfile) {
  initHideStyles.innerText = '.disco_avg_rating { opacity: 0 !important; }';
}
document.head.appendChild(initHideStyles);

const createHideButton = function createHideButton(hideable, buttonEl) {
  const buttonContainer = document.createElement('div');
  buttonContainer.style.float = 'left';
  buttonEl.id = 'show_rating_btn';
  buttonEl.setAttribute('hiding', 'true');
  buttonEl.innerText = 'Show Ratings';

  buttonEl.addEventListener('click', (event) => {
    event.preventDefault();

    if (buttonEl.getAttribute('hiding') === 'true') {
      hideable.forEach((hidden) => {
        hidden.classList.remove('tm-hidden-rating');
      });
      buttonEl.setAttribute('hiding', 'false');
      buttonEl.innerText = 'Hide Ratings';
    } else {
      hideable.forEach((hidden) => {
        hidden.classList.add('tm-hidden-rating');
      });
      buttonEl.setAttribute('hiding', 'true');
      buttonEl.innerText = 'Show Ratings';
    }
  });

  buttonContainer.appendChild(buttonEl);
  return buttonContainer;
}

const getHideableOnReleasePage = function getHideableOnReleasePage() {
  const hideable = [];
  hideable.push(document.querySelector('.avg_rating'));
  hideable.push(document.querySelector('.avg_rating_friends'));

  // 'ranked'
  const infoRows = document.querySelectorAll('.album_info > tbody > tr');
  infoRows.forEach((infoRow) => {
    const rowHead = infoRow.querySelector('th.info_hdr');
    if (rowHead.innerText === 'Ranked') {
      hideable.push(infoRow);
    }
  });

  const trackRatings = document.querySelectorAll('.track_rating');
  trackRatings.forEach((trackRating) => {
    hideable.push(trackRating);
  });

  return hideable;
};

const setupHideStyles = function setupHideStyles() {
  const hideStyles = document.createElement('style');
  hideStyles.id = 'initial-hide-styles';
  hideStyles.innerText = '.tm-hidden-rating { opacity: 0 !important; }';
  document.head.appendChild(hideStyles);
};

const setupReleasePage = function setupReleasePage() {
  const ratingNum = document.querySelector('.my_catalog_rating > .rating_num');
  if (ratingNum.innerText === '---') {
    const hideable = getHideableOnReleasePage();
    hideable.forEach((hidden) => {
      hidden.classList.add('tm-hidden-rating');
    });

    // create button
    const buttonEl = document.createElement('div');
    buttonEl.classList.add('more_btn');
    const buttonContainer = createHideButton(hideable, buttonEl);
    const buttonRow = document.querySelector('.release_my_catalog');
    const clearButton = buttonRow.querySelector('.clear');
    buttonRow.insertBefore(buttonContainer, clearButton);
  }
};

const getHideableOnProfilePage = function getHideableOnProfilePage() {
  const hideable = [];

  const releases = document.querySelectorAll('.disco_release');
  releases.forEach((release) => {
    const rating = release.querySelector('.disco_cat_inner');
    if (!rating) {
      const releaseAvg = release.querySelector('.disco_avg_rating');
      hideable.push(releaseAvg);
    }
  });

  return hideable;
};

const setupProfilePage = function setupProfilePage() {
  const hideable = getHideableOnProfilePage();
  if (hideable.length) {
    hideable.forEach((hidden) => {
      hidden.classList.add('tm-hidden-rating');
    });

    const buttonEl = document.createElement('a');
    buttonEl.href = '#';
    const buttonContainer = createHideButton(hideable, buttonEl);
    const artistInfo = document.querySelector('.artist_info');

    const clear = document.createElement('div');
    clear.style.clear = 'both';
    artistInfo.appendChild(clear);

    const header = document.createElement('div');
    header.innerText = 'Show / Hide Ratings';
    header.classList.add('info_hdr');
    header.style.marginTop = '1em';
    artistInfo.appendChild(header);

    const content = document.createElement('div');
    content.classList.add('info_content');
    content.appendChild(buttonContainer);
    artistInfo.appendChild(content);
  }
};

document.addEventListener('DOMContentLoaded', () => {
  initHideStyles.remove();
  setupHideStyles();
  if (isRelease) {
    setupReleasePage();
  } else if (isProfile) {
    setupProfilePage();
  }
});