forked from Revadike/Misc-JavaScript-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DIM - Auto Ghost Switcher.user.js
159 lines (139 loc) · 5.59 KB
/
DIM - Auto Ghost Switcher.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// ==UserScript==
// @name DIM - Auto Ghost Switcher [Steam only]
// @description DIM - Auto Ghost Switcher [Steam only]
// @namespace Revadike
// @author Revadike
// @version 1.0.0
// @include https://app.destinyitemmanager.com/*/d2/inventory
// @connect steamcommunity.com
// @grant GM.xmlHttpRequest
// @grant unsafeWindow
// ==/UserScript==
// ==Config==
const STEAMID3 = 82699538; // steamID3, get from https://steamid.io/lookup/
const INTERVAL = 10; // seconds, time between checking current in-game status
// ==/Config==
// ==Code==
(() => {
"use strict";
function pickGhost(ghosts, o_area) {
var area = o_area.toLowerCase();
var ghost = ghosts.default;
switch (true) {
case area.includes("edz"):
case area.includes("earth"):
ghost = ghosts.edzplus || ghosts.edz || ghosts.default;
break;
case area.includes("titan"):
ghost = ghosts.titanplus || ghosts.titan || ghosts.default;
break;
case area.includes("nessus"):
ghost = ghosts.nessusplus || ghosts.nessus || ghosts.default;
break;
case o_area.includes("Io"):
ghost = ghosts.ioplus || ghosts.io || ghosts.default;
break;
case area.includes("mercury"):
ghost = ghosts.mercuryplus || ghosts.mercury || ghosts.default;
break;
case area.includes("mars"):
case area.includes("hellas basin"):
ghost = ghosts.marsplus || ghosts.mars || ghosts.default;
break;
case area.includes("tangled shore"):
ghost = ghosts.tangledplus || ghosts.tangled || ghosts.default;
break;
case area.includes("dreaming city"):
ghost = ghosts.dreamingplus || ghosts.dreaming || ghosts.default;
break;
case area.includes("vanguard"):
case area.includes("strike"):
ghost = ghosts.strikesplus || ghosts.strike || ghosts.default;
break;
case area.includes("crucible"):
ghost = ghosts.crucibleplus || ghosts.crucible || ghosts.default;
break;
case area.includes("gambit"):
ghost = ghosts.gambitplus || ghosts.gambit || ghosts.default;
break;
case area.includes("leviathan"):
ghost = ghosts.leviathanplus || ghosts.leviathan || ghosts.default;
break;
case area.includes("moon"):
ghost = ghosts.moonplus || ghosts.moon || ghosts.default;
break;
}
console.log("Equipping ghost: " + ghost.name);
ghost.equip();
}
function getD2Area(doc) {
var elem = doc.querySelector(".rich_presence");
return elem ? elem.innerText.trim() : null;
}
function monitorInGameStatus(ghosts) {
var currentArea = null;
var monitor = setInterval(() => {
GM.xmlHttpRequest({
method: "GET",
url: "https://steamcommunity.com/miniprofile/" + STEAMID3 + "?t=" + Date.now(),
onload: (response) => {
var html = response.responseText;
var parser = new DOMParser();
var doc = parser.parseFromString(html, "text/html")
var area = getD2Area(doc);
if (!area || currentArea === area) { return; }
console.log(area);
currentArea = area;
pickGhost(ghosts, area);
}
});
}, INTERVAL * 1000);
}
function doubleClick(elem) {
var event = new MouseEvent("dblclick", {
view: unsafeWindow,
bubbles: true,
cancelable: true
});
elem.dispatchEvent(event);
}
function initGhostSwitcher() {
console.log("DIM ready");
var ghosts = {};
var rows = document.querySelectorAll(".store-row");
for (var row of rows) {
if (!row.querySelector("[aria-label=Ghost]")) { continue; }
var items = row.querySelectorAll(".item");
for (let item of items) {
var text = item.querySelector("span");
if (!text) { continue; }
var aoe = text.innerText.replace("+", "plus"); // area of effect
if (!aoe) { continue; }
var equip = () => doubleClick(item);
var name = item.title.trim().replace(/(\r\n\t|\n|\r\t)/gm, " ");;
var node = item;
var ghost = { equip, aoe, name, ghost };
ghosts[aoe] = ghosts[aoe] || ghost; // keep first, assuming best is first
ghosts.default = ghosts.default || ghost;
}
}
var arr = Object.values(ghosts);
console.log("Found " + arr.length + " special ghosts:\n" + arr.map(i => i.name).join("\n"));
monitorInGameStatus(ghosts);
}
function observeDIM() {
var observer = new MutationObserver((records) => {
for (var record of records) {
for (var node of record.removedNodes) {
if (node.classList && node.classList.contains("exit-done")) {
initGhostSwitcher();
return;
}
}
}
});
observer.observe(document.getElementById('app'), { childList: true, subtree: true });
}
observeDIM();
})();
// ==/Code==