Greasy Fork

来自缓存

Greasy Fork is available in English.

恢复 Tiktok 无版权音乐播放

优化性能,避免重复加载和设置

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         恢复 Tiktok 无版权音乐播放
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  优化性能,避免重复加载和设置
// @author       wzj042
// @match        https://www.tiktok.com/*/video/*
// @grant        none
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tiktok.com
// @run-at       document-end
// @license      MIT
// ==/UserScript==

(function () {
  function setVideoToPlay() {
    var musicLink = document.querySelector("h4 a");
    var copyright = musicLink
      ? musicLink.href === "https://www.tiktok.com/"
      : false;

    if (copyright) {
      var video = document.querySelector("video");

      if (video && video.muted && video.volume > 0) {
        video.muted = false;
        // 尝试播放视频
        video.play().catch((e) => {
          if (e.name !== "NotAllowedError") {
            console.error(e);
          }
        });
      }
      // 更新来源
      if (musicLink && !musicLink.dataset.labelSet) {
        var script = document.getElementById(
          "__UNIVERSAL_DATA_FOR_REHYDRATION__"
        );
        if (script) {
          var data = JSON.parse(script.textContent || script.innerText);
          var music =
            data.__DEFAULT_SCOPE__["webapp.video-detail"].itemInfo.itemStruct
              .music;
          var musicLabel = `${music.title}-${music.authorName}`;
          musicLink.innerText = musicLabel;
          musicLink.dataset.labelSet = true; // 标记已设置
        }
      }
    }
  }

  setInterval(setVideoToPlay, 1000);
})();