-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcx-bhavcopy-datatables.js
More file actions
157 lines (139 loc) · 5.56 KB
/
Copy pathmcx-bhavcopy-datatables.js
File metadata and controls
157 lines (139 loc) · 5.56 KB
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
// ==UserScript==
// @name MCX Bhavcopy Datatables
// @namespace https://github.com/codeaditya/browser-userscripts
// @downloadURL https://cdn.jsdelivr.net/gh/codeaditya/browser-userscripts@main/mcx-bhavcopy-datatables.js
// @updateURL https://cdn.jsdelivr.net/gh/codeaditya/browser-userscripts@main/mcx-bhavcopy-datatables.js
// @version 2025.02.16
// @description Use Datatables to show MCX Bhavcopy data
// @author Aditya <code.aditya at gmail>
// @match https://www.mcxindia.com/market-data/bhavcopy
// @grant GM_getResourceText
// @grant GM_addStyle
// @grant unsafeWindow
// @resource datatablesCSS https://cdn.datatables.net/2.2.2/css/dataTables.dataTables.min.css
// @require https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js
// @require https://cdn.datatables.net/2.2.2/js/dataTables.min.js
// ==/UserScript==
// https://eslint.org/docs/latest/use/configure/language-options#specifying-globals
/* global jQuery, $, DataTable */
(function () {
"use strict";
GM_addStyle(GM_getResourceText("datatablesCSS"));
let myDataTable;
let pollingInterval;
let pollingTimeout;
let lastPolledJSON = "";
const pollingIntervalDuration = 500;
const pollingTimeoutDuration = 30_000;
const renderInt = DataTable.render.number(",", ".", 0);
const renderFloat = DataTable.render.number(",", ".", 2);
const bhavCopyColumns = [
{ data: "Date", title: "Raw Date" },
{ data: "InstrumentName", title: "Instrument" },
{ data: "Symbol", title: "Symbol" },
{ data: "ExpiryDate", title: "Expiry" },
{ data: "OptionType", title: "Option Type" },
{ data: "StrikePrice", title: "Strike Price", render: renderFloat },
{ data: "Open", title: "Open", render: renderFloat },
{ data: "High", title: "High", render: renderFloat },
{ data: "Low", title: "Low", render: renderFloat },
{ data: "Close", title: "Close", render: renderFloat },
{ data: "PreviousClose", title: "Prev Close", render: renderFloat },
{ data: "Volume", title: "Volume (Lots)", render: renderInt },
{ data: "VolumeInThousands", title: "Volume (K)", className: "dt-right" },
{ data: "Value", title: "Value (Lacs)", render: renderFloat },
{ data: "OpenInterest", title: "OI (Lots)", render: renderInt },
];
const myEnhancedTableElement = document.createElement("table");
myEnhancedTableElement.id = "my-enhanced-table";
myEnhancedTableElement.className = "display";
(document.querySelector(".contents.bhavcopy") || document.body).appendChild(
myEnhancedTableElement
);
const initializeDataTable = (data) => {
myDataTable?.destroy();
myDataTable = new DataTable(myEnhancedTableElement, {
data: data,
columns: bhavCopyColumns,
pageLength: 10,
ordering: true,
searching: true,
deferRender: true,
processing: true,
});
console.debug("DataTable updated.");
};
// varName can be vBC or vBCCW
const checkDataUpdate = (varName) => {
console.debug(`Polling for ${varName} change.`);
const currentData = unsafeWindow[varName];
if (!currentData) {
console.info(`${varName} is undefined or null in polling check.`);
return;
}
const currentJSON = JSON.stringify(currentData);
if (currentJSON !== lastPolledJSON) {
console.debug(`${varName} content changed! Updating DataTable.`);
lastPolledJSON = currentJSON;
initializeDataTable(currentData);
clearInterval(pollingInterval); // Stop polling after update
clearTimeout(pollingTimeout); // Clear the timeout as well
pollingInterval = null;
pollingTimeout = null;
console.debug(`Polling stopped for ${varName} change.`);
}
};
const handleButtonClick = (e, varName) => {
e.preventDefault();
// Clear any existing polling interval and timeout
if (pollingInterval) {
clearInterval(pollingInterval);
pollingInterval = null;
console.debug("Previous polling interval cleared.");
}
if (pollingTimeout) {
clearTimeout(pollingTimeout);
pollingTimeout = null;
console.debug("Previous polling timeout cleared.");
}
// Begin polling for changes
lastPolledJSON = JSON.stringify(unsafeWindow[varName] ?? "");
pollingInterval = setInterval(() => {
checkDataUpdate(varName);
}, pollingIntervalDuration);
// Set timeout to stop polling after a duration
pollingTimeout = setTimeout(() => {
if (pollingInterval) {
clearInterval(pollingInterval);
pollingInterval = null;
pollingTimeout = null;
console.warn(`Polling timed out for ${varName} change.`);
}
}, pollingTimeoutDuration);
};
document
.getElementById("btnShowDatewise")
.addEventListener("click", (e) => handleButtonClick(e, "vBC"));
document
.getElementById("btnShowCommoditywise")
.addEventListener("click", (e) => handleButtonClick(e, "vBCCW"));
const originalDateWiseTableContainer = $("#cont-1");
const originalCommodityWiseTableContainer = $("#cont-2");
const toggleElement = document.querySelector(".maToggle");
toggleElement.addEventListener("click", (e) => {
e.preventDefault();
originalDateWiseTableContainer.toggle();
originalCommodityWiseTableContainer.toggle();
if (toggleElement.classList.contains("active")) {
initializeDataTable(unsafeWindow.vBC ?? []);
} else {
initializeDataTable(unsafeWindow.vBCCW ?? []);
}
});
if (unsafeWindow.vBC) {
originalDateWiseTableContainer.toggle();
initializeDataTable(unsafeWindow.vBC);
} else {
console.info("vBC data not found on the page.");
}
})();