-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetflix_Interactive_Selection_Tracker.user.js
80 lines (68 loc) · 2.76 KB
/
Netflix_Interactive_Selection_Tracker.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// ==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/file-saver@2.0.0/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");
}
})();