Hide RYM Ratings If Unrated

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

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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();
  }
});