-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemrise_auto_ignore.user.js
161 lines (141 loc) · 4.57 KB
/
memrise_auto_ignore.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
160
161
// ==UserScript==
// @name Memrise Auto Ignore
// @namespace https://techno-coder.github.io
// @version 0.1
// @description Identifies already learned words and ignores them
// @author Technocoder
// @match https://www.memrise.com/course/*
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.notification
// @require https://code.jquery.com/jquery-3.3.1.min.js
// @require https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js
// ==/UserScript==
let $ = window.jQuery;
const KEY_STORE = "collected_items";
(() => {
if (is_garden_page()) return;
setup_authentication();
$(".dropdown-menu").append(`<li><a id="auto_ignore"><i class="ico"></i> Auto Ignore</a></li>`);
$(".dropdown-menu").append(`<li><a id="collect"><i class="ico ico-water"></i> Collect</a></li>`);
if (is_course_page()) {
$("#collect").click(async () => collect_notification(await collect_course()));
$("#auto_ignore").click(async () => ignore_notification(await ignore_course()));
} else {
$("#collect").click(async () => collect_notification(await collect_level($(".thing"))));
$("#auto_ignore").click(async () => ignore_notification(await ignore_level($(".thing"), stripped_url())));
}
})();
function setup_authentication() {
$.ajaxSetup({
beforeSend: (header) => {
let token = Cookies.get("csrftoken");
header.setRequestHeader("X-CSRFToken", token);
}
});
}
function stripped_url() {
let current_url = window.location.href;
return current_url.substr(0, current_url.lastIndexOf('/'));
}
function is_course_page() {
let url = stripped_url();
let page_identifier = url.substr(url.lastIndexOf('/') + 1);
return isNaN(page_identifier);
}
function is_garden_page() {
return window.location.href.includes("garden");
}
async function ignore_course() {
let ignore_count = 0;
let promises = [];
$(".level").each(async (_index, level) => {
let level_url = $(level).attr("href");
promises.push($.get(level_url, async (page_data) => {
let page = $($.parseHTML(page_data));
let items = page.find(".thing");
ignore_count += await ignore_level(items, level_url);
}));
});
await Promise.all(promises);
return ignore_count;
}
async function ignore_level(items, level_url) {
let key_store = await get_key_store();
let ignore_count = 0;
items.each((_index, item) => {
let item_status = $(item).children(".thinguser").children();
if (not_learned(item_status)) {
let key = $(item).children(".col_a").children(".text").text();
if (key_store.has(key)) {
$(item).find(":checkbox").prop("checked", true);
ignore_count++;
}
}
});
simulate_save(items, level_url);
return ignore_count;
}
async function simulate_save(items, level_url) {
let ignore_data = [];
items.each((_index, item) => {
ignore_data.push({
learnable_id: parseInt($(item).attr("data-learnable-id"), 10),
ignored: $(item).find(":checkbox").prop("checked"),
});
});
let ignore_data_string = JSON.stringify(ignore_data);
let payload = { ignore_data: ignore_data_string };
await $.post(`${level_url}/ajax/ignore_learnables/`, payload);
}
async function collect_course() {
let collect_count = 0;
let promises = [];
$(".level").each(async (_index, level) => {
let level_url = $(level).attr("href");
promises.push($.get(level_url, async (page_data) => {
let page = $.parseHTML(page_data);
collect_count += await collect_level($(page).find(".thing"));
}));
});
await Promise.all(promises);
return collect_count;
}
async function collect_level(items) {
let key_store = await get_key_store();
let collect_count = 0;
items.each((_index, item) => {
let item_status = $(item).children(".thinguser").children();
if (not_learned(item_status) || item_status.text() == "Ignored") return;
let key = $(item).children(".col_a").children(".text").text();
if (!key_store.has(key)) {
key_store.add(key);
collect_count++;
}
});
await GM.setValue(KEY_STORE, JSON.stringify(Array.from(key_store)));
return collect_count;
}
async function get_key_store() {
let raw_key_store = await GM.getValue(KEY_STORE, "[]");
return new Set(JSON.parse(raw_key_store));
}
function not_learned(item_status) {
return item_status.hasClass("ico-purple");
}
function ignore_notification(ignore_count) {
let notification = {
text: `Ignored ${ignore_count} already learned words`,
title: "Memrise Auto Ignore",
timeout: "3000",
};
GM.notification(notification);
}
function collect_notification(collect_count) {
let notification = {
text: `Added ${collect_count} newly learned words`,
title: "Memrise Auto Ignore",
timeout: "3000",
};
GM.notification(notification);
}