forked from ageitgey/show-facebook-computer-vision-tags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook.js
96 lines (80 loc) · 2.69 KB
/
facebook.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
const locale = (Array.from(document.body.classList).find(cls => cls.match(/^Locale_/)));
/**
* Update CV tags
* @param localeData
*/
const show_facebook_cv_tags = function(localeData) {
const images = [...document.getElementsByTagName('img')];
const localeRegex = new RegExp(localeData.separator_regex, 'i');
images.forEach(el => {
if (el.hasAttribute('data-prev-alt') &&
el.getAttribute('data-prev-alt') === el.getAttribute('alt')) {
return;
}
el.setAttribute('data-prev-alt', el.alt);
const altText = el.alt;
const isCVTag = altText.startsWith(localeData.tag_prefix);
if (isCVTag) {
const tags = altText.slice(localeData.tag_prefix.length).split(localeRegex);
let html = '<ul class="sfcvt">';
tags.forEach(function(tag) {
let prefix = '∙';
if (tag in localeData.emoji_map) {
prefix = localeData.emoji_map[tag];
} else if (tag.endsWith(localeData.tag_ends_with)) {
prefix = localeData.emoji_map[localeData.tag_ends_with_map];
}
html += `<li>${prefix} ${tag}</li>`;
});
html += '</ul>';
el.style.position = 'relative';
el.insertAdjacentHTML('afterend', html);
}
});
};
/**
* Initialize the plugin
* @param localeData
*/
const initializePlugin = function(localeData) {
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => show_facebook_cv_tags(localeData));
});
const config = {
attributes: true,
childList: true,
characterData: false
};
observer.observe(document.body, config);
show_facebook_cv_tags(localeData);
};
/**
* Make the fetch request to get locale data
* @param localePath
*/
const makeLocaleRequest = function(localePath) {
fetch(chrome.extension.getURL(localePath))
.then(response => response.json())
.then(data => initializePlugin(data))
.catch(err => console.error('FB COMPUTER VISION TAGS ERROR', err));
};
/**
* Check for locale
*/
if (locale === 'Locale_es_LA') {
makeLocaleRequest('/locales/es/messages.json');
} else if (locale === 'Locale_de_DE') {
makeLocaleRequest('/locales/de/messages.json');
} else if (locale === 'Locale_en_GB' || locale === 'Locale_en_US') {
makeLocaleRequest('/locales/en_US/messages.json');
} else if (locale === 'Locale_fr_FR') {
makeLocaleRequest('/locales/fr/messages.json');
} else if (locale === 'Locale_it_IT') {
makeLocaleRequest('/locales/it_IT/messages.json');
} else if (locale === 'Locale_vi_VN') {
makeLocaleRequest('/locales/vi_VN/messages.json');
} else if (locale === 'Locale_th_TH') {
makeLocaleRequest('/locales/th_TH/messages.json');
} else if (locale === 'Locale_pt_BR') {
makeLocaleRequest('/locales/pt_BR/messages.json')
}