禁用 WebRTC

禁用 WebRTC 防止泄露真实ip,默认禁用 WebRTC,脚本菜单选项用于启用/禁用当前域名的 WebRTC

当前为 2024-09-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 禁用 WebRTC
  3. // @version 1.0
  4. // @description 禁用 WebRTC 防止泄露真实ip,默认禁用 WebRTC,脚本菜单选项用于启用/禁用当前域名的 WebRTC
  5. // @author ChatGPT
  6. // @match *://*/*
  7. // @grant GM_setValue
  8. // @grant GM_getValue
  9. // @grant GM_registerMenuCommand
  10. // @run-at document-start
  11. // @namespace https://greasyfork.org/users/452911
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // 获取启用站点列表
  18. const enabledSites = GM_getValue('enabledSites', []);
  19. const currentURL = window.location.hostname;
  20.  
  21. // 设置菜单命令
  22. const menuCommandLabel = enabledSites.includes(currentURL)
  23. ? '设置当前域名禁用WebRTC'
  24. : '设置当前域名启用WebRTC';
  25.  
  26. GM_registerMenuCommand(menuCommandLabel, function() {
  27. if (enabledSites.includes(currentURL)) {
  28. GM_setValue('enabledSites', enabledSites.filter(site => site !== currentURL));
  29. } else {
  30. GM_setValue('enabledSites', [...enabledSites, currentURL]);
  31. }
  32. });
  33.  
  34. // 默认禁用 WebRTC
  35. function disableWebRTC() {
  36. Object.defineProperty(window, 'RTCPeerConnection', {
  37. value: function() { throw new Error('RTCPeerConnection is disabled'); }
  38. });
  39.  
  40. Object.defineProperty(window, 'webkitRTCPeerConnection', {
  41. value: function() { throw new Error('webkitRTCPeerConnection is disabled'); }
  42. });
  43.  
  44. Object.defineProperty(window, 'mozRTCPeerConnection', {
  45. value: function() { throw new Error('mozRTCPeerConnection is disabled'); }
  46. });
  47.  
  48. Object.defineProperty(window, 'msRTCPeerConnection', {
  49. value: function() { throw new Error('msRTCPeerConnection is disabled'); }
  50. });
  51.  
  52. if (typeof navigator.mediaDevices !== 'undefined') {
  53. Object.defineProperty(navigator.mediaDevices, 'getUserMedia', {
  54. value: function() { throw new Error('getUserMedia is disabled'); }
  55. });
  56. }
  57. }
  58.  
  59. // 根据当前域名的状态启用或禁用 WebRTC
  60. if (!enabledSites.includes(currentURL)) {
  61. disableWebRTC();
  62. }
  63. })();