-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathmenu.js
More file actions
140 lines (127 loc) · 4.07 KB
/
menu.js
File metadata and controls
140 lines (127 loc) · 4.07 KB
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
import { addRateLater } from '../utils.js';
import { frontendUrl } from '../config.js';
const i18n = chrome.i18n;
function get_current_tab_video_id() {
function get_tab_video_id(tabs) {
for (let tab of tabs) {
// only one tab is returned
var video_id = new URL(tab.url).searchParams.get('v');
if (video_id == null || video_id === '') {
return Promise.reject(new Error('not a video id'));
}
return video_id;
}
}
return new Promise((resolve) => {
chrome.tabs.query({ active: true, currentWindow: true }, resolve);
}).then(get_tab_video_id);
}
/**
* Open the Tournesol home page.
*/
function openTournesolHome() {
chrome.tabs.create({
url: `${frontendUrl}?utm_source=extension&utm_medium=menu`,
});
}
/**
* Open the Tournesol comparison page in a new tab.
*/
function rateNowAction(event) {
const button = event.target;
get_current_tab_video_id().then(
(videoId) => {
chrome.tabs.create({
url: `${frontendUrl}/comparison?uidA=yt:${videoId}&utm_source=extension&utm_medium=menu`,
});
},
() => {
button.disabled = true;
button.setAttribute('data-error', 'Not a YouTube video page.');
}
);
}
/**
* Add the video to the user's rate-later list.
*
* If the user is not logged, display the login iframe.
*/
function addToRateLaterAction(event) {
const button = event.target;
get_current_tab_video_id().then(
async (videoId) => {
button.disabled = true;
const { success, message } = await addRateLater(videoId);
if (success) {
button.setAttribute('data-success', message);
} else {
// ask the content script to display the modal containing the login iframe
chrome.tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
chrome.tabs.sendMessage(
tabs[0].id,
{ message: 'displayModal' },
function (response) {
if (!response.success) {
button.setAttribute('data-error', 'Failed.');
}
}
);
}
);
}
},
() => {
button.disabled = true;
button.setAttribute('data-error', 'Not a YouTube video page.');
}
);
}
/**
* Open the Tournesol entity analysis page in a new tab.
*/
function openAnalysisPageAction(event) {
const button = event.target;
get_current_tab_video_id().then(
(videoId) => {
chrome.tabs.create({
url: `${frontendUrl}/entities/yt:${videoId}?utm_source=extension&utm_medium=menu`,
});
},
() => {
button.disabled = true;
button.setAttribute('data-error', 'Not a YouTube video page.');
}
);
}
function openOptionsPage() {
chrome.runtime.openOptionsPage();
}
function rateLaterHistory() {
chrome.tabs.create({
url: 'https://www.youtube.com/feed/history',
});
}
/**
* Create the action menu.
*/
document.addEventListener('DOMContentLoaded', function () {
const tournesolHomeLink = document.getElementById('tournesol_home');
const rateNowButton = document.getElementById('rate_now');
const rateLaterButton = document.getElementById('rate_later');
const analysisButton = document.getElementById('analysis');
const preferencesButton = document.getElementById('preferences');
const rateLaterHistoryButton = document.getElementById('rate_later_history');
tournesolHomeLink.addEventListener('click', openTournesolHome);
rateNowButton.addEventListener('click', rateNowAction);
rateLaterButton.addEventListener('click', addToRateLaterAction);
analysisButton.addEventListener('click', openAnalysisPageAction);
preferencesButton.addEventListener('click', openOptionsPage);
rateLaterHistoryButton.addEventListener('click', rateLaterHistory);
rateNowButton.textContent = i18n.getMessage('menuRateNow');
rateLaterButton.textContent = i18n.getMessage('menuRateLater');
analysisButton.textContent = i18n.getMessage('menuAnalysis');
preferencesButton.textContent = i18n.getMessage('menuPreferences');
rateLaterHistoryButton.textContent = i18n.getMessage('menuRateLaterHistory');
});