mirror of
https://github.com/tubearchivist/browser-extension.git
synced 2024-11-22 11:40:13 +00:00
check channel subscribed
This commit is contained in:
parent
4112501900
commit
adde4c51c0
@ -138,10 +138,15 @@ async function subscribe(url) {
|
|||||||
async function videoExists(id) {
|
async function videoExists(id) {
|
||||||
const path = `api/video/${id}/`;
|
const path = `api/video/${id}/`;
|
||||||
let response = await sendGet(path);
|
let response = await sendGet(path);
|
||||||
console.log(response);
|
|
||||||
return Boolean(response.data);
|
return Boolean(response.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getChannel(channelHandle) {
|
||||||
|
const path = `api/channel/search/?q=${channelHandle}`;
|
||||||
|
let response = await sendGet(path);
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
|
|
||||||
async function cookieStr(cookieLines) {
|
async function cookieStr(cookieLines) {
|
||||||
const path = 'api/cookie/';
|
const path = 'api/cookie/';
|
||||||
let payload = {
|
let payload = {
|
||||||
@ -226,6 +231,9 @@ function handleMessage(request, sender, sendResponse) {
|
|||||||
case 'videoExists': {
|
case 'videoExists': {
|
||||||
return await videoExists(request.videoId);
|
return await videoExists(request.videoId);
|
||||||
}
|
}
|
||||||
|
case 'getChannel': {
|
||||||
|
return await getChannel(request.channelHandle);
|
||||||
|
}
|
||||||
default: {
|
default: {
|
||||||
let err = new Error(`unknown message type ${JSON.stringify(request.type)}`);
|
let err = new Error(`unknown message type ${JSON.stringify(request.type)}`);
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
@ -106,8 +106,20 @@ function getBrowser() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getChannelContainers() {
|
function getChannelContainers() {
|
||||||
let nodes = document.querySelectorAll('#inner-header-container, #owner');
|
const elements = document.querySelectorAll('#inner-header-container, #owner');
|
||||||
return nodes;
|
const channelContainerNodes = [];
|
||||||
|
|
||||||
|
elements.forEach(element => {
|
||||||
|
if (isElementVisible(element)) {
|
||||||
|
channelContainerNodes.push(element);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return channelContainerNodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isElementVisible(element) {
|
||||||
|
return element.offsetWidth > 0 || element.offsetHeight > 0 || element.getClientRects().length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function ensureTALinks() {
|
function ensureTALinks() {
|
||||||
@ -201,7 +213,7 @@ function buildChannelButtonDiv() {
|
|||||||
|
|
||||||
function buildChannelSubButton(channelHandle) {
|
function buildChannelSubButton(channelHandle) {
|
||||||
let channelSubButton = document.createElement('span');
|
let channelSubButton = document.createElement('span');
|
||||||
channelSubButton.innerText = 'Subscribe';
|
channelSubButton.innerText = 'Checking...';
|
||||||
channelSubButton.title = `TA Subscribe: ${channelHandle}`;
|
channelSubButton.title = `TA Subscribe: ${channelHandle}`;
|
||||||
channelSubButton.setAttribute('data-id', channelHandle);
|
channelSubButton.setAttribute('data-id', channelHandle);
|
||||||
channelSubButton.setAttribute('data-type', 'channel');
|
channelSubButton.setAttribute('data-type', 'channel');
|
||||||
@ -211,19 +223,36 @@ function buildChannelSubButton(channelHandle) {
|
|||||||
console.log(`subscribe to: ${channelHandle}`);
|
console.log(`subscribe to: ${channelHandle}`);
|
||||||
sendUrl(channelHandle, 'subscribe', channelSubButton);
|
sendUrl(channelHandle, 'subscribe', channelSubButton);
|
||||||
});
|
});
|
||||||
channelSubButton.addEventListener('mouseover', () => {
|
|
||||||
checkChannelSubscribed(channelHandle);
|
|
||||||
});
|
|
||||||
Object.assign(channelSubButton.style, {
|
Object.assign(channelSubButton.style, {
|
||||||
padding: '5px',
|
padding: '5px',
|
||||||
cursor: 'pointer',
|
cursor: 'pointer',
|
||||||
});
|
});
|
||||||
|
checkChannelSubscribed(channelSubButton);
|
||||||
|
|
||||||
return channelSubButton;
|
return channelSubButton;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkChannelSubscribed(channelHandle) {
|
function checkChannelSubscribed(channelSubButton) {
|
||||||
console.log(`check channel subscribed: ${channelHandle}`);
|
function handleResponse(message) {
|
||||||
|
if (
|
||||||
|
message === false ||
|
||||||
|
(typeof message === 'object' && message.channel_subscribed === false)
|
||||||
|
) {
|
||||||
|
channelSubButton.innerText = 'Subscribe';
|
||||||
|
} else if (typeof message === 'object' && message.channel_subscribed === true) {
|
||||||
|
channelSubButton.innerText = 'Unsubscribe';
|
||||||
|
} else {
|
||||||
|
console.log('Unknown state');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function handleError() {
|
||||||
|
console.log('error');
|
||||||
|
}
|
||||||
|
|
||||||
|
let channelHandle = channelSubButton.dataset.id;
|
||||||
|
let message = { type: 'getChannel', channelHandle };
|
||||||
|
let sending = sendMessage(message);
|
||||||
|
sending.then(handleResponse, handleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildSpacer() {
|
function buildSpacer() {
|
||||||
|
Loading…
Reference in New Issue
Block a user