update readme
[dotfiles/.git] / .config / BraveSoftware / Brave-Browser / afalakplffnnnlkncjhbmahjfjhmlkal / 1.0.28 / 1 / scripts / brave_rewards / publisher / youtube / youtube.bundle.js
1 !function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";r.r(t);const n=(e,t,r)=>{if(void 0===e||void 0===t||e.length<t.length)return"";const n=e.indexOf(t);if(-1===n)return"";const o=n+t.length,i=e.indexOf(r,o);let u="";return i!==o?u=-1!==i&&i>o||-1!==i?e.substring(o,i):e.substring(o):""===r&&(u=e.substring(o)),u},o=e=>`https://www.youtube.com/channel/${e}/videos`,i=e=>"youtube_"+e,u=e=>"youtube#channel:"+e,a=e=>{if(!e)return"";let t=n(e,'"avatar":{"thumbnails":[{"url":"','"');return t||(t=n(e,'"width":88,"height":88},{"url":"','"'),t||"")},s=e=>{if(!e)return"";let t=n(e,'"ucid":"','"');return t||(t=n(e,'HeaderRenderer":{"channelId":"','"'),t||(t=n(e,'<link rel="canonical" href="https://www.youtube.com/channel/','">'),t||(t=n(e,'browseEndpoint":{"browseId":"','"'),t||"")))},c=()=>document.querySelector("#contents .ytp-title-link"),l=e=>{if(!e||!e.href)return"";const t=new URL(e.href);return t?h(t):""},h=e=>{if(!e)return"";const t=new URLSearchParams(e.search);return t&&t.get("v")||""},d=e=>!(!e||!e.endsWith("/watch"));let f=null,p=!1,b=!0;const m=e=>{f&&f.postMessage({type:"GreaselionError",mediaType:"youtube",data:{errorMessage:e}})},y=e=>{if(!e)return void m("Invalid channel id");const t=document.querySelector("#channel-container #text-container");if(!t)return void m("Unable to extract channel name from page");const r=u(e),n=(s=t)?s.innerText.trim():"";var s;const h=(e=>{for(const t of e){const e=a(t.text);if(e)return e}return""})(document.scripts);let d="";const p=c(),b=l(p);b&&(d=i(b));const y=o(e);f&&f.postMessage({type:"SavePublisherVisit",mediaType:"youtube",data:{url:y,publisherKey:r,publisherName:n,mediaKey:d,favIconUrl:h}})},w=()=>{const e=location.href;fetch(e).then(e=>{if(!e.ok)throw new Error(`YouTube publisher request failed: ${e.statusText} (${e.status})`);return e.text()}).then(e=>{const t=s(e);if(!t)return void m("Unable to scrape channel id");const r=u(t),h=(e=>{if(!e)return"";const t=n(e,'<meta itemprop="name" content="','"');return t||""})(e),d=a(e);let p="";const b=c(),y=l(b);y&&(p=i(y));const w=o(t);f&&f.postMessage({type:"SavePublisherVisit",mediaType:"youtube",data:{url:w,publisherKey:r,publisherName:h,mediaKey:p,favIconUrl:d}})}).catch(e=>{throw new Error("YouTube fetch request failed: "+e)})},g=(e,t,r,c)=>{const l=a(t),d=s(t),p=u(d),b=h(new URL(e));if(!b)return;const m=i(b);r||(r=(e=>{if(!e)return"";const t=n(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)),c||(c=o(d)),f&&f.postMessage({type:"SavePublisherVisit",mediaType:"youtube",data:{url:c,publisherKey:p,publisherName:r,mediaKey:m,favIconUrl:l}})},v=()=>{const e=location.href,t=h(new URL(e));if(!t)return;const r=(e=>"https://www.youtube.com/watch?v="+e)(t),n=encodeURI(r),o={};fetch("https://www.youtube.com/oembed?format=json&url="+n).then(t=>{if(!t.ok){if(401!==t.status)throw new Error(`YouTube oembed request failed: ${t.statusText} (${t.status})`);(e=>{fetch(e).then(e=>{if(!e.ok)throw new Error(`YouTube publisher request failed: ${e.statusText} (${e.status})`);return e.text()}).then(t=>{g(e,t,"","")}).catch(e=>{throw new Error("YouTube fetch request failed: "+e)})})(e)}return t.json()}).then(e=>(o.publisherUrl=e.author_url,o.publisherName=e.author_name,fetch(o.publisherUrl))).then(e=>{if(!e.ok)throw new Error(`YouTube publisher request failed: ${e.statusText} (${e.status})`);return e.text()}).then(t=>{g(e,t,o.publisherName,o.publisherUrl)}).catch(e=>{throw new Error("YouTube fetch request failed: "+e)})},x=()=>{if(d(location.pathname))return void v();if((e=location.pathname)&&e.includes("/channel/")){const e=(e=>{if(!e)return"";const t=n(e+"/","/channel/","/");if(!t)return"";const r=t.split("?");return r&&0!==r.length?r[0]:""})(location.pathname);return void y(e)}var e;if((e=>!(!e||!e.includes("/user/")))(location.pathname))return void w();if((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))return void(f&&f.postMessage({type:"SavePublisherVisit",mediaType:"",data:{url:"https://www.youtube.com",publisherKey:"youtube.com",publisherName:"youtube.com",favIconUrl:""}}));const t=(e=>{for(const t of e){let e=s(t.text);if(e)return e}return""})(document.scripts);y(t)},T=e=>{const t=new URLSearchParams(e.search),r=(e=>e&&e.get("docid")||"")(t),n=i(r),o=(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 o=r.split(",");if(!o||0===o.length)return 0;if(n.length!==o.length)return 0;let i=0;for(let e=0;e<n.length;++e){const t=parseFloat(n[e]),r=parseFloat(o[e]);i+=Math.round(r-t)}return i})(t);f&&(f.postMessage({type:"MediaDurationMetadata",mediaType:"youtube",data:{mediaKey:n,duration:o,firstVisit:b}}),b=!1)},S=()=>{p||(p=!0,f&&(f.postMessage({type:"RegisterOnCompletedWebRequest",mediaType:"youtube",data:{urlPatterns:["https://www.youtube.com/api/stats/watchtime?*"]}}),f.onMessage.addListener(e=>{switch(e.type){case"OnCompletedWebRequest":((e,t)=>{if("youtube"!==e)return;if(!t||!t.url)return;const r=new URL(t.url);T(r)})(e.mediaType,e.details)}})))};chrome.extension.inIncognitoContext||(f=chrome.runtime.connect("jidkidbbcafjabdphckchenhfomhnfma",{name:"Greaselion"}),document.addEventListener("readystatechange",(function(){"complete"!==document.readyState||"visible"!==document.visibilityState||d(location.pathname)||setTimeout(()=>{S(),x()},200)})),document.addEventListener("visibilitychange",(function(){"visible"===document.visibilityState&&(S(),x())})),document.addEventListener("yt-page-data-updated",(function(){"visible"===document.visibilityState&&(S(),x()),b=!0})),console.info("Greaselion script loaded: youtube.ts"))}]);