amnrst / youtube-like-to-view-ratio

11 stars 1 forks source link

Views HTML changed #12

Open ericandrewlewis opened 1 year ago

ericandrewlewis commented 1 year ago

The CSS selector for the YT views has changed, so the extension currently doesn't work.

The current selector is

const viewsEl = document.querySelector('#above-the-fold #description-inner tp-yt-paper-tooltip #tooltip');

but the current HTML is

<div id="above-the-fold" class="style-scope ytd-watch-metadata">
  <div id="bottom-row" class="style-scope ytd-watch-metadata">
    <div id="description" class="item style-scope ytd-watch-metadata">
      <div id="description-inner" class="style-scope ytd-watch-metadata">
        <div id="info-container" class="style-scope ytd-watch-metadata">
          <yt-formatted-string id="info" class="style-scope ytd-watch-metadata"><span dir="auto"
              class="bold style-scope yt-formatted-string" style-target="bold">2.4K views</span><span dir="auto"
              class="bold style-scope yt-formatted-string" style-target="bold"> </span><span dir="auto"
              class="bold style-scope yt-formatted-string" style-target="bold">3 years ago</span></yt-formatted-string>
          <dom-if class="style-scope ytd-watch-metadata"><template is="dom-if"></template></dom-if>
        </div>
      </div>
    </div>
  </div>
</div>

Here is a suggestion for a modification to the getStats() function to calculate the views

const infoContainerEl = document.querySelector('#above-the-fold #description-inner #info-container');
const found = infoContainerEl.innerText.match(/([^\s]*) views/);
if (found === null) {
    return null;    
}
const displayedNumber = found[1];
let views;
if (displayedNumber[displayedNumber.length - 1] === 'K') {
    const a = parseFloat(displayedNumber.substring(0, displayedNumber.length - 1));
    views = a * 1000;
} else if (displayedNumber[displayedNumber.length - 1] === 'M') {
    const a = parseFloat(displayedNumber.substring(0, displayedNumber.length - 1));
    views = a * 1000000;
} else if (displayedNumber[displayedNumber.length - 1] === 'B') {
    const a = parseFloat(displayedNumber.substring(0, displayedNumber.length - 1));
    views = a * 1000000000;
} else {
    views = parseInt(displayedNumber);
}