starRating.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { cLog } from "./utils";
  2. function createStarRating(rating, isMobile) {
  3. let starRating = document.createElement('label');
  4. let starSlider = document.createElement("input");
  5. starSlider.setAttribute("class", "rating");
  6. starSlider.setAttribute("max", "5");
  7. starSlider.setAttribute("readonly", "");
  8. starSlider.setAttribute("style", `--fill:rgb(255, 215, 0);--value:${rating.toString()};};background-color: transparent;`);
  9. starSlider.setAttribute("type", "range");
  10. starRating.appendChild(starSlider);
  11. let YTLikeButton;
  12. if (isMobile){
  13. YTLikeButton = document.querySelector('#app > div.page-container > ytm-watch > ytm-single-column-watch-next-results-renderer > ytm-slim-video-metadata-section-renderer > ytm-slim-video-action-bar-renderer > div > ytm-slim-metadata-toggle-button-renderer:nth-child(1)');
  14. } else {
  15. YTLikeButton = document.querySelector('#top-level-buttons-computed > ytd-toggle-button-renderer:nth-child(1)');
  16. }
  17. YTLikeButton.insertAdjacentElement('afterend', starRating);
  18. try {
  19. let YTBar = document.querySelector('#ryd-bar-container');
  20. YTBar.setAttribute("style", "width: 190%; height: 2px;");
  21. }
  22. catch(err) {
  23. cLog("RateBar Not Present");
  24. }
  25. let style = `<style>
  26. .rating {
  27. --dir: right;
  28. --fill: gold;
  29. --fillbg: rgba(100, 100, 100, 0.15);
  30. --star: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 17.25l-6.188 3.75 1.641-7.031-5.438-4.734 7.172-0.609 2.813-6.609 2.813 6.609 7.172 0.609-5.438 4.734 1.641 7.031z"/></svg>');
  31. --stars: 5;
  32. --starSize: 2.8rem;
  33. --symbol: var(--star);
  34. --value: 1;
  35. --w: calc(var(--stars) * var(--starSize));
  36. --x: calc(100% * (var(--value) / var(--stars)));
  37. block-size: var(--starSize);
  38. inline-size: var(--w);
  39. position: relative;
  40. touch-action: manipulation;
  41. -webkit-appearance: none;
  42. }
  43. [dir="rtl"] .rating {
  44. --dir: left;
  45. }
  46. .rating::-moz-range-track {
  47. background: linear-gradient(to var(--dir), var(--fill) 0 var(--x), var(--fillbg) 0 var(--x));
  48. block-size: 100%;
  49. mask: repeat left center/var(--starSize) var(--symbol);
  50. }
  51. .rating::-webkit-slider-runnable-track {
  52. background: linear-gradient(to var(--dir), var(--fill) 0 var(--x), var(--fillbg) 0 var(--x));
  53. block-size: 100%;
  54. mask: repeat left center/var(--starSize) var(--symbol);
  55. -webkit-mask: repeat left center/var(--starSize) var(--symbol);
  56. }
  57. .rating::-moz-range-thumb {
  58. height: var(--starSize);
  59. opacity: 0;
  60. width: var(--starSize);
  61. }
  62. .rating::-webkit-slider-thumb {
  63. height: var(--starSize);
  64. opacity: 0;
  65. width: var(--starSize);
  66. -webkit-appearance: none;
  67. }
  68. .rating,
  69. .rating-label {
  70. display: block;
  71. font-family: ui-sans-serif, system-ui, sans-serif;
  72. }
  73. .rating-label {
  74. margin-block-end: 1rem;
  75. }
  76. </style>`;
  77. document.head.insertAdjacentHTML("beforeend", style);
  78. }
  79. export { createStarRating };