-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexport-saved-trade-history.user.js
147 lines (130 loc) · 5.22 KB
/
export-saved-trade-history.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
// ==UserScript==
// @name Cryptohopper Export Saved Trade History
// @namespace https://github.com/markrickert/cryptohopper-dashboard-watchlist
// @version 0.3
// @description Adds single-click export functionality to the trade history page using the saved settings (after saving at least once), and allows for saving and loading export settings. The author of this script finds it useful to set the to part of the date range to sometime in the future when using this functionality, such as 01/01/2030 12:00 AM.
// @author @eatsleepcoderepeat-gl
// @homepage https://github.com/markrickert/cryptohopper-dashboard-watchlist
// @updateURL https://github.com/markrickert/cryptohopper-dashboard-watchlist/raw/main/export-saved-trade-history.user.js
// @match https://www.cryptohopper.com/trade-history
// @icon https://www.google.com/s2/favicons?domain=cryptohopper.com
// @grant GM_addStyle
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
try {
// Only run this code on the intended page(s) (useful when @required in a parent script)
if (["/trade-history"].includes(window.location.pathname))
(function () {
"use strict";
const EXPORT_KEY = "export-trade-history-settings";
const EXPORT_BUTTON_NAME = "#export-saved-trade-history";
const SAVE_BUTTON_NAME = "#save-export-settings";
const LOAD_BUTTON_NAME = "#load-export-settings";
const BUTTON_PRIMARY_CLASS = "btn btn-primary waves-effect waves-light";
const BUTTON_SECONDARY_CLASS = "btn btn-default waves-effect";
var buttonsAdded = false;
// This function loads the currently saved settings
function loadSavedSettings() {
var exportSettings = JSON.parse(GM_getValue(EXPORT_KEY));
// Apply saved settings
$("#export_type").val(exportSettings.format);
$("#check_sells").prop("checked", exportSettings.buys);
$("#check_buys").prop("checked", exportSettings.sells);
$("#export_daterange").val(exportSettings.daterange);
}
// This function sets our CSS
function setStyles() {
GM_addStyle(`
button${EXPORT_BUTTON_NAME},
button${SAVE_BUTTON_NAME} {
margin-right: 3px;
}
button${LOAD_BUTTON_NAME} {
margin-right: 2px;
}
`);
}
// This function adds the Export Saved button and handles click events
function exportButtonHandler() {
if (
!$(EXPORT_BUTTON_NAME).length &&
GM_getValue(EXPORT_KEY, false) !== false
) {
// Add the Export Saved button
$(`button[onclick="jQuery('#exportDiv').toggle()"]`).before(
`<button type="button" id="${EXPORT_BUTTON_NAME.replace(
"#",
""
)}" class="${BUTTON_PRIMARY_CLASS}"><i class="fa fa-download m-r-5"></i> Export Saved</button>`
);
// Handle clicks of the Export Saved button
$(EXPORT_BUTTON_NAME).on("click", function () {
loadSavedSettings();
startExport();
});
buttonsAdded = true;
}
}
// This function saves the current settings when exporting
function saveSettingsButtonHandler() {
// Add the Save Settings button
$('button[onclick="startExport()"]').before(
`<button type="button" id="${SAVE_BUTTON_NAME.replace(
"#",
""
)}" class="${BUTTON_PRIMARY_CLASS}">Save Settings</button>`
);
// Handle clicks of the Save Settings button
$(SAVE_BUTTON_NAME).on("click", function () {
var format = $("#export_type").val();
var buys = $("#check_sells").prop("checked");
var sells = $("#check_buys").prop("checked");
var daterange = $("#export_daterange").val();
// Save these values for future use
GM_setValue(
EXPORT_KEY,
JSON.stringify({
format: format,
buys: buys,
sells: sells,
daterange: daterange,
})
);
// If this is the first time saving, add the Export Saved and Load Settings buttons
if (!buttonsAdded) {
exportButtonHandler();
loadSettingsButtonHandler();
}
});
}
// This function loads the currently saved settings
function loadSettingsButtonHandler() {
if (
!$(LOAD_BUTTON_NAME).length &&
GM_getValue(EXPORT_KEY, false) !== false
) {
// Add the Load Settings button
$(SAVE_BUTTON_NAME).before(
`<button type="button" id="${LOAD_BUTTON_NAME.replace(
"#",
""
)}" class="${BUTTON_SECONDARY_CLASS}">Load Settings</button>`
);
// Handle clicks of the Load Settings button
$(LOAD_BUTTON_NAME).on("click", loadSavedSettings);
buttonsAdded = true;
}
}
jQuery(() => {
setStyles();
exportButtonHandler();
saveSettingsButtonHandler();
loadSettingsButtonHandler();
});
})();
} catch (err) {
console.log(
`Error in script export-saved-trade-history.user.js: ${err.name}: ${err.message}`
);
}