IMDB - Open Reviews in New Tab

Restores the functionality of opening reviews in a new tab on IMDB movie pages.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         IMDB - Open Reviews in New Tab
// @version      0.0.1
// @description  Restores the functionality of opening reviews in a new tab on IMDB movie pages.
// @author       You
// @match        https://www.imdb.com/title/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=imdb.com
// @grant        none
// @license      MIT
// @namespace https://greasyfork.org/users/846945
// ==/UserScript==

(function () {
  "use strict";

  const id = window.location.pathname.split("/").at(-2);
  if (!id.startsWith("tt")) return;

  console.log("Running IMDB - Open Reviews in New Tab script");

  const reviewsAnchor = document.querySelector(
    "section[data-testid='UserReviews'] a.ipc-title-link-wrapper"
  );
  if (reviewsAnchor) {
    reviewsAnchor.addEventListener(
      "click",
      (e) => {
        e.preventDefault();
        e.stopImmediatePropagation();
        window.open(window.location.origin + `/title/${id}/reviews`, "_blank");
      },
      { capture: true }
    );
  } else {
    console.error("Reviews anchor not found");
  }

  const nextData = document.getElementById("__NEXT_DATA__");
  if (!nextData) {
    console.error("__NEXT_DATA__ not found");
    return;
  }

  const nextDataJson = JSON.parse(nextData.innerText);
  const reviews =
    nextDataJson?.props?.pageProps?.mainColumnData?.featuredReviews?.edges;
  if (!reviews || reviews.length === 0) {
    console.error("No featured reviews found in __NEXT_DATA__");
    return;
  }

  document
    .querySelectorAll(
      "div[data-testid='user-reviews-summary-featured-review-card']"
    )
    .forEach((card, i) => {
      card.addEventListener(
        "click",
        (e) => {
          e.preventDefault();
          e.stopImmediatePropagation();
          window.open(
            window.location.origin +
              `/title/${id}/reviews/?featured=${reviews[i].node.id}`
          );
        },
        { capture: true }
      );
    });
})();