Netflix Interactive Selection Tracker

Logs every option selected during interactive content

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Netflix Interactive Selection Tracker
// @namespace    https://github.com/lmelvin
// @version      1.1
// @license      MIT
// @description  Logs every option selected during interactive content
// @author       lmelvin
// @match        https://www.netflix.com/watch/*
// @grant        GM_addStyle
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/FileSaver.min.js
// ==/UserScript==

(function () {
  'use strict';

  let DOWNLOAD_MENU = `<li class='dm list-header'>Interactive Selection Tracker</li>
                       <li class='dm download track'>Download</li>`;
  let DM_HEADER_SELECTOR = '.dm.header';
  let DM_DOWNLOAD_SELECTOR = '.dm.download';

  let observerConfig = { attributes: true, childList: true, subtree: true };
  let currentLog;

  GM_addStyle(`.dm { white-space: nowrap; padding-right: 5px; }
               ${DM_HEADER_SELECTOR} { font-weight: bold; }
               ${DM_DOWNLOAD_SELECTOR} { cursor: pointer }`);

  var observerCallback = function (mutations, observer) {
    for (var mutation of mutations) {
      buildDownloadMenu(mutation);
      if (mutation.isSelectionMutation()) {
        appendToCurrentLog(mutation.getSelectedValue());
        break;
      }
    }
  };

  let observer = new MutationObserver(observerCallback);
  observer.observe(document.body, observerConfig);

  function buildDownloadMenu(mutation) {
    for (var node of mutation.addedNodes) {
      let trackMenu = (node.parentNode || node).querySelector('.audio-subtitle-controller');
      if (trackMenu !== null && trackMenu.querySelector('.subtitle-downloader-menu') === null) {
        let ul = document.createElement('ul');
        ul.setAttribute('class', 'track-list');
        ul.innerHTML = DOWNLOAD_MENU;
        trackMenu.appendChild(ul);
        ul.querySelector(DM_DOWNLOAD_SELECTOR).addEventListener('click', download);
      }
    }
  }

  function appendToCurrentLog(value) {
    currentLog = (currentLog ? currentLog + `\n` : '') + value;
    console.log(currentLog);
  }

  function download() {
    var blob = new Blob([currentLog], { type: "text/plain;charset=utf-8" });
    saveAs(blob, `${getTitle()}_selections.txt`);
  }

  function getTitle() {
    let titleElement = document.querySelector('h4.ellipsize-text');
    let title = titleElement.textContent;
    title = title ? title.replace(/[:*?"<>|\\\/]+/g, '_').replace(/ /g, '.') : 'selection_tracker';
    return title;
  }

  MutationRecord.prototype.isSelectionMutation = function () {
    return this.type == 'attributes'
      && this.target.classList.contains('BranchingInteractiveScene--choice-selection')
      && this.target.classList.contains('selected');
  }

  MutationRecord.prototype.getSelectedValue = function () {
    return this.target.getAttribute("aria-label");
  }
})();