notes/.config/BraveSoftware/Brave-Browser/Greaselion/Temp/scoped_dirm6QxMM/youtubeBase.bundle.js

1 line
No EOL
6.5 KiB
JavaScript

(()=>{"use strict";const e="mnojpmjdmbbfmejpflffifhffcmidifd";let t=null;const r=()=>t;let n=!1;const i=(e,t,i)=>{if(!e||n)return;n=!0;const o=r();o&&(o.postMessage({type:"RegisterOnCompletedWebRequest",mediaType:e,data:{urlPatterns:[t]}}),o.onMessage.addListener((e=>{"OnCompletedWebRequest"===e.type&&i(e.mediaType,e.details)})))},o=(e,t)=>e&&t?`${e}_${t}`:"",a=(e,t)=>`${e}#channel:${t}`,s=(e,t,r)=>{if(e.length<t.length)return"";const n=e.indexOf(t);if(-1===n)return"";const i=n+t.length,o=e.indexOf(r,i);let a="";return o!==i?a=-1!==o&&o>i||-1!==o?e.substring(i,o):e.substring(i):""===r&&(a=e.substring(i)),a},u=(e,t)=>`${e}: ${t.statusText} (${t.status})`,c={"&amp;":"&","&lt;":"<","&gt;":">","&quot;":'"',"&#39;":"'"},l=/&(?:amp|lt|gt|quot|#(0+)?39);/g,h=RegExp(l.source),d="youtube",m="youtube.com",f=e=>`https://www.youtube.com/channel/${e}/videos`,p=e=>{if(!e)return"";let t=s(e,'"avatar":{"thumbnails":[{"url":"','"');return t||(t=s(e,'"width":88,"height":88},{"url":"','"'),t||"")},w=e=>{if(!e)return"";let t=s(e,'"ucid":"','"');return t||(t=s(e,'HeaderRenderer":{"channelId":"','"'),t||(t=s(e,'<link rel="canonical" href="https://www.youtube.com/channel/','">'),t||(t=s(e,'browseEndpoint":{"browseId":"','"'),t||"")))},b=()=>document.querySelector("#contents .ytp-title-link"),y=e=>{if(!e||!e.href)return"";const t=new URL(e.href);return t?g(t):""},g=e=>{if(!e)return"";const t=new URLSearchParams(e.search);return t&&t.get("v")||""},v=e=>!(!e||!e.endsWith("/watch"));let E=!0;const I=e=>{E=e},U=async()=>{const e=location.href,t=await fetch(e);if(!t.ok){const e=u("Publisher request failed",t);throw new Error(e)}const r=await t.text(),n=w(r);if(!n)throw new Error("Invalid channel id");const i=a(d,n),m=(e=>{if(!e)return"";const t=s(e,'<meta itemprop="name" content="','"');return t?(r=t)&&h.test(r)?r.replace(l,(e=>c[e]||"'")):r||"":"";var r})(r);if(!m)throw new Error("Invalid publisher name");const g=b(),v=y(g),E=o(d,v||n),I=p(r);return{url:f(n),publisherKey:i,publisherName:m,mediaKey:E,favIconUrl:I}},x=(e,t,r,n)=>{const i=w(t);if(!i)throw new Error("Invalid channel id");const u=a(d,i),c=g(new URL(e));if(!c)throw new Error("Invalid media id");const l=o(d,c||i);if(!r&&!(r=(e=>{if(!e)return"";const t=s(e,'"author":"','"');if(!t)return"";let r=null;try{r=JSON.parse(`{"brave_publisher":"${t}"}`)}catch(e){throw new Error(`Error parsing publisher name from response: ${e}`)}return r.brave_publisher})(t)))throw new Error("Invalid publisher name");return n||(n=f(i)),{url:n,publisherKey:u,publisherName:r,mediaKey:l,favIconUrl:p(t)}},K=()=>{$().then((e=>{const t=r();if(!t)throw new Error("Invalid port");t.postMessage({type:"SavePublisherVisit",mediaType:d,data:{url:e.url,publisherKey:e.publisherKey,publisherName:e.publisherName,mediaKey:e.mediaKey,favIconUrl:e.favIconUrl}})})).catch((e=>{throw new Error(`Failed to retrieve publisher data: ${e}`)}))},$=async()=>{if(v(location.pathname))return(async()=>{const e=location.href,t=g(new URL(e));if(!t)throw new Error("Invalid media id");const r=(e=>`https://www.youtube.com/watch?v=${e}`)(t),n=encodeURI(r),i=await fetch(`https://www.youtube.com/oembed?format=json&url=${n}`);if(!i.ok){if(401===i.status)return(async e=>{const t=await fetch(e);if(!t.ok){const e=u("Publisher request failed",t);throw new Error(e)}const r=await t.text();if(!r)throw new Error("Publisher request failed: empty response");return x(e,r,"","")})(e);{const e=u("oEmbed request failed",i);throw new Error(e)}}const o=await i.json(),a={};a.publisherUrl=o.author_url,a.publisherName=o.author_name;const s=await fetch(a.publisherUrl);if(!s.ok){const e=u("Publisher request failed",i);throw new Error(e)}const c=await s.text();if(!c)throw new Error("Publisher request failed: empty response");return x(e,c,a.publisherName,a.publisherUrl)})();if((e=location.pathname)&&e.includes("/channel/")){const e=(e=>{if(!e)return"";const t=s(e+"/","/channel/","/");if(!t)return"";const r=t.split("?");return r&&0!==r.length?r[0]:""})(location.pathname);return(async e=>{if(!e)throw new Error("Invalid channel id");const t=document.querySelector("#channel-container #text-container");if(!t)throw new Error("Unable to extract channel name from page");const r=a(d,e),n=(i=t)?i.innerText.trim():"";var i;if(!n)throw new Error("Invalid publisher name");const s=b(),u=y(s),c=o(d,u||e);return{url:f(e),publisherKey:r,publisherName:n,mediaKey:c,favIconUrl:(e=>{for(const t of e){const e=p(t.text);if(e)return e}return""})(document.scripts)}})(e)}var e;return(e=>!(!e||!e.includes("/user/")))(location.pathname)?U():(e=>{const t=["/","/account","/account_advanced","/account_billing","/account_notifications","/account_playback","/account_privacy","/account_sharing","/channel","/feed","/gaming","/oops","/pair","/playlist","/premium","/reporthistory","/subscription_manager","/user","/watch"];if(!e)return!1;const r=(e=>{if(!e)return"";let t=e.substring(0,e.indexOf("/",1));return t&&t!==e||(t=e.substring(0,e.indexOf("?",1)),t&&t!==e||(t=e)),t})(e);for(const e of t)if(r===e)return!0;return!1})(location.pathname)?{url:"https://www.youtube.com",publisherKey:m,publisherName:m,mediaKey:"",favIconUrl:""}:U()},q="https://www.youtube.com/api/stats/watchtime?*",R=(e,t)=>{if(e!==d)return;if(!t||!t.url)return;(e=>{const t=new URLSearchParams(e.search),n=(e=>e&&e.get("docid")||"")(t),i=o(d,n),a=(e=>{if(!e)return 0;const t=e.get("st"),r=e.get("et");if(!t||!r)return 0;const n=t.split(",");if(!n||0===n.length)return 0;const i=r.split(",");if(!i||0===i.length)return 0;if(n.length!==i.length)return 0;let o=0;for(let e=0;e<n.length;++e){const t=parseFloat(n[e]),r=parseFloat(i[e]);o+=Math.round(r-t)}return o})(t),s=r();s&&(s.postMessage({type:"MediaDurationMetadata",mediaType:d,data:{mediaKey:i,duration:a,firstVisit:E}}),I(!1))})(new URL(t.url))};var _;chrome.extension.inIncognitoContext||(_=e=>{e?(document.addEventListener("readystatechange",(function(){"complete"!==document.readyState||"visible"!==document.visibilityState||v(location.pathname)||setTimeout((()=>{i(d,q,R),K()}),200)})),document.addEventListener("visibilitychange",(function(){"visible"===document.visibilityState&&(i(d,q,R),K())})),document.addEventListener("yt-page-data-updated",(function(){"visible"===document.visibilityState&&(i(d,q,R),K()),I(!0)}))):console.error("Failed to initialize communications port")},t?_(!0):(chrome.runtime.sendMessage(e,{type:"SupportsGreaselion"},(function(r){!chrome.runtime.lastError&&r&&r.supported&&(t=chrome.runtime.connect(e,{name:"Greaselion"}),_(!0))})),setTimeout((()=>{t||(t=chrome.runtime.connect("jidkidbbcafjabdphckchenhfomhnfma",{name:"Greaselion"}),_(!0))}),100)),console.info("Greaselion script loaded: youtubeBase.ts"))})();