-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter.user.js
91 lines (79 loc) · 2.69 KB
/
twitter.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
// ==UserScript==
// @name Twitter User Note
// @namespace http://github.com/mirogta/
// @version 0.0.1
// @description Add notes to Twitter users
// @author mirogta
// @license MIT
// @homepageURL https://github.com/mirogta/tampermonkey-twitter-usernote
// @match https://twitter.com/*
// @inject-into content
// @grant GM_getValue
// @grant GM_setValue
// @compatible firefox >=14
// @compatible chrome >=18
// ==/UserScript==
// ==OpenUserJS==
// @author mirogta
// ==/OpenUserJS==
(function() {
'use strict';
console.log(`Initialising Twitter User Note`);
const data = {
noteId: '_note',
};
let ob = new window.MutationObserver(function() {
console.debug(`- content changed`);
checkAndAddInputField();
});
function checkAndAddInputField() {
// find user section and add input field
const mainElement = document.getElementsByTagName('main');
if(mainElement && mainElement[0]) {
const container = mainElement[0].querySelector('a[role="link"]');
const noteEl = document.getElementById(data.noteId);
if(container && container.nextElementSibling && null == noteEl) {
console.log(`- adding user note input field`);
const newNoteEl = document.createElement('textarea');
newNoteEl.id = data.noteId;
newNoteEl.placeholder = 'Add custom note...';
newNoteEl.style = 'margin-top:10px; position: absolute; top: 2px; left: 160px;';
newNoteEl.addEventListener('change', saveNote, false);
container.nextElementSibling.append(newNoteEl);
}
loadNote();
}
}
function getUserUrl() {
const openGraphUrl = document.head.querySelector('meta[property="og:url"]');
if(openGraphUrl) {
return openGraphUrl.content;
}
return null;
}
function loadNote() {
const userUrl = getUserUrl();
const noteEl = document.getElementById(data.noteId);
if(noteEl && userUrl) {
const content = GM_getValue(userUrl);
if(content) {
console.log(`- found user note for ${userUrl}`);
noteEl.value = content;
} else {
noteEl.value = '';
}
}
}
function saveNote(event) {
const userUrl = getUserUrl();
if(userUrl) {
const content = event.target.value;
GM_setValue(userUrl, content);
console.log(`- saved user note for ${userUrl}`);
}
}
ob.observe(document, {
childList: true,
subtree: true,
});
})();