forked from LeoDupont/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazon-notepad.js
225 lines (197 loc) · 7.47 KB
/
amazon-notepad.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// ==UserScript==
// @name Amazon Notepad
// @namespace https://github.com/BrandonR541/userscripts
// @version 0.1
// @description Lets you annotate Amazon products. Uses localStorage.
// @author LeoDupont
// @match https://www.amazon.fr/*
// @match https://www.amazon.de/*
// @match https://www.amazon.es/*
// @match https://www.amazon.it/*
// @match https://www.amazon.nl/*
// @match https://www.amazon.se/*
// @match https://www.amazon.co.uk/*
// @match https://www.amazon.com/*
// @match https://www.amazon.ca/*
// @match https://www.amazon.mx/*
// @match https://www.amazon.br/*
// @match https://www.amazon.jp/*
// @match https://www.amazon.in/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=amazon.fr
// @grant none
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
// ==/UserScript==
////////////////////////////////////////////////////////////////////
// Amazon Notepad
//
// Displays a simple text input under the product's name
// * on product pages (always),
// * on search results pages (if notes were found).
//
// Notes are saved on 'change' event.
// => the input need to loose focus for the event to be triggered,
// => a simple Tab or a click away will do the job.
//
// Uses the browser's localStorage to store notes.
// => notes are not synced anywhere on the Internet.
////////////////////////////////////////////////////////////////////
var $ = window.$; // Declaration
(function() {
'use strict';
// === Product Page ===
var pageId = getProductIdFromUrl(document.URL);
if (pageId) {
var pageNotes = loadProductNotes(pageId);
showNotepad(pageId, pageNotes, '#titleSection');
}
// === Search Results Page ===
$('h2.a-size-mini > a.a-link-normal').each(function(i, elm) {
var productId = getProductIdFromUrl(elm.href);
if (productId) {
var productNotes = loadProductNotes(productId);
if (productNotes) {
showNotepad(productId, productNotes, $(elm).parent().parent());
}
}
});
})();
// =======================================================
// == Helpers
// =======================================================
function getProductIdFromUrl(url) {
var match = url.match(/\/(\w{10})/);
if (match) {
return match[1];
}
}
// =======================================================
// == Original UI Function from LeoDupont
// =======================================================
/* function showNotepad(productId, value, elmToAppendTo) {
$('<input>')
.attr({
id: 'amazon-notepad-' + productId,
name: 'amazon-notepad',
style: 'width: 100%; margin: 10px 0; background-color: lightyellow;',
value: value,
placeholder: 'Amazon Notepad...',
})
.appendTo(elmToAppendTo)
.change(function(e) {
var newNotes = e.target.value;
console.log('[AMAZON NOTEPAD] Storing new notes for ' + productId + ': ' + newNotes);
saveProductNotes(productId, newNotes);
});
} */
// =======================================================
// == Tweaked UI Funtion:
// =======================================================
//
// -> Toggle switch added to hide note section
// Pages with no notes default to hidden
// Pages with notes default to open
// Search results only show the toggle for pages with notes and defaults to open
//
// -> Changed from single line input to multiline
// Displays all lines instead of using a scroll bar
// Text input area automatically grows in height as you type
//
// -> Color and font changes due to personal preference
//
// =======================================================
function showNotepad(productId, value, elmToAppendTo) {
//--------------------------
// Toggle Switch Background
//--------------------------
const switchContainer = $('<div>')
.attr({
id: 'amazon-notepad-switch-' + productId,
style: 'display: inline-block;\
vertical-align: middle;\
position: relative;\
width: 50px;\
height: 20px;\
background-color: #fadaeb;\
border-radius: 10px;\
cursor: pointer;',
})
.appendTo(elmToAppendTo);
//--------------------------
// Toggle Switch Handle
//--------------------------
const switchHandle = $('<div>')
.attr({
id: 'amazon-notepad-switch-handle-' + productId,
style: 'position: absolute;\
top: 2px;\
left: 2px;\
width: 16px;\
height: 16px;\
background-color: #b19aa6;\
border-radius: 50%;\
transition: left 0.2s ease;',
})
.appendTo(switchContainer);
//--------------------------
// Text Field Style
//--------------------------
const textarea = $('<textarea>')
.attr({
id: 'amazon-notepad-' + productId,
name: 'amazon-notepad',
style: 'display: none;\
width: 100%;\
margin: 10px 0;\
background-color: #fa8dc7;\
color: black;\
font-size: 17px;\
resize: none;\
overflow: hidden;',
placeholder: 'Amazon Notepad...',
})
.val(value)
.appendTo(elmToAppendTo);
//--------------------------
// Text Field Display Logic
//--------------------------
const handleWidth = switchHandle.outerWidth();
const containerWidth = switchContainer.outerWidth();
const handleLeftPosition = containerWidth - handleWidth - 2;
const toggleNotepad = function() {
const textareaEl = textarea[0];
const handleEl = switchHandle[0];
if (textareaEl.style.display === 'none') {
textareaEl.style.display = '';
textareaEl.style.height = ''; // Reset the height first
textareaEl.style.height = `${textareaEl.scrollHeight}px`; // Set the height to fit the content
handleEl.style.left = `${handleLeftPosition}px`;
} else {
textareaEl.style.display = 'none';
handleEl.style.left = '2px';
}
};
switchContainer.on('click', toggleNotepad);
textarea.on('input', function(e) {
const textareaEl = e.target;
textareaEl.style.height = ''; // Reset the height first
textareaEl.style.height = `${textareaEl.scrollHeight}px`; // Set the height to fit the content
const newNotes = e.target.value;
console.log('[AMAZON NOTEPAD] Storing new notes for ' + productId + ': ' + newNotes);
saveProductNotes(productId, newNotes);
});
// Expand the textarea initially to fit the content if there is existing text
if (value.trim() !== '') {
textarea.trigger('input');
switchHandle.css('left', `${handleLeftPosition}px`);
textarea.show().trigger('input'); // Trigger input event explicitly
}
}
// =======================================================
// == Storage
// =======================================================
function loadProductNotes(productId) {
return localStorage.getItem('amazon-notepad-' + productId);
}
function saveProductNotes(productId, notes) {
localStorage.setItem('amazon-notepad-' + productId, notes);
}