YouTube → Supabase Logger

Inserts each YouTube video ID into your Supabase table exactly once

当前为 2025-05-08 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        YouTube → Supabase Logger
// @description Inserts each YouTube video ID into your Supabase table exactly once
// @match       https://www.youtube.com/
// @run-at      document-end
// @version 0.0.1.20250508200230
// @namespace https://greasyfork.org/users/1435046
// ==/UserScript==

(function() {
  'use strict';

  // Supabase configuration
  const SUPABASE_URL = 'https://haughsijawbsqwumuryg.supabase.co';
  const SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImhhdWdoc2lqYXdic3F3dW11cnlnIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MzM0ODE3MjYsImV4cCI6MjA0OTA1NzcyNn0.stESUMuJEs4CNBWGtxZr1XNp2XpnQeXmKkq3fNaVE-c';
  const TABLE       = 'youtube_recommended_videos_table';

  // Remember which IDs we've already sent
  const seen = new Set();

  // Extracts the “v” parameter from a YouTube URL
  function getVideoId(href) {
    try {
      const u = new URL(href);
      return u.searchParams.get('v');
    } catch {
      return null;
    }
  }

  // Inserts a new video ID into Supabase
  function insertVideoId(id) {
    fetch(`${SUPABASE_URL}/rest/v1/${TABLE}`, {
      method:  'POST',
      headers: {
        'Content-Type':  'application/json',
        'apikey':        SUPABASE_KEY,
        'Authorization': `Bearer ${SUPABASE_KEY}`
      },
      body: JSON.stringify([{ video_id_column: id }])
    });
  }

  // Scan for new video links and send unseen IDs to Supabase
  function logAndSend() {
    document.querySelectorAll('a[href*="/watch"]').forEach(link => {
      const id = getVideoId(link.href);
      if (id && !seen.has(id)) {
        seen.add(id);
        insertVideoId(id);
      }
    });
  }

  // Initial run
  logAndSend();

  // Observe for added nodes
  new MutationObserver(logAndSend)
    .observe(document.body, { childList: true, subtree: true });

})();