首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> C++ 入門

Chrome 插件開發實戰:通過 Hook API 破解網頁防複製限制

     編輯:C++ 入門

問題背景

許多網站(如 360doc、yu1u.org)通過 JS 禁用右鍵菜單、選擇、複製等操作:

document.body.oncontextmenu = () => false;
document.body.onselectstart = () => false;
document.body.oncopy = () => false;
// 甚至監聽鍵盤事件、覆蓋選區等

解決方案對比

插件架構:Manifest V2

{
  "name": "UnBlock Copy",
  "manifest_version": 2,
  "version": "1.0.0",
  "description": "解除網頁複製限制",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "content_scripts": [{
    "matches": ["https://*/*", "https://*/*"],
    "js": ["js/hookapi.js"],
    "run_at": "document_end",
    "all_frames": true
  }]
}

核心 Hook 腳本:hookapi.js

// 需要解除綁定的事件列表
const events = [
  'oncontextmenu', 'ondragstart', 'onselectstart',
  'onselect', 'onbeforecopy', 'oncopy'
];

// 保存原始屬性描述符
const originalDescriptors = new Map();

function hookEvent(eventName) {
  const descriptor = Object.getOwnPropertyDescriptor(Document.prototype, eventName) ||
                     Object.getOwnPropertyDescriptor(HTMLElement.prototype, eventName) ||
                     Object.getOwnPropertyDescriptor(Element.prototype, eventName) ||
                     Object.getOwnPropertyDescriptor(Node.prototype, eventName);
  if (!descriptor) return;
  
  originalDescriptors.set(eventName, descriptor);
  
  // 重新定義屬性:setter 直接忽略賦值
  Object.defineProperty(document.body, eventName, {
    set: function(val) { /* 吞掉賦值,不綁定任何處理器 */ },
    get: function() { return null; },
    configurable: true
  });
}

// 執行 Hook
events.forEach(hookEvent);

// 額外:移除已綁定的屬性(針對內聯腳本已執行的情況)
events.forEach(e => document.body[e] = null);

關鍵技術點

  1. 原型鏈查找:事件屬性可能定義在 Document/HTMLElement/Element/Node 上,需逐級查找
  2. Object.defineProperty:攔截 setter,阻止賦值生效
  3. run_at: document_end:確保在頁面腳本執行前/中注入,但內聯腳本可能已執行,故需額外清空 body[event]=null
  4. all_frames: true:覆蓋 iframe 內頁面

安裝測試流程

  1. Chrome 打開 chrome://extensions/
  2. 開啟「開發者模式」
  3. 點擊「加載已解壓的擴展程序」,選擇插件目錄
  4. 測試目標網站:右鍵、選擇、複製應恢復正常

進階:Manifest V3 遷移

MV3 中 content_scripts 機制不變,但需注意 host_permissions 聲明。核心 Hook 邏輯不變。

道德與法律提示

此技術僅供學習瀏覽器擴展開發與 JS 原型鏈機制理解,請尊重網站版權與服務條款,勿用於非法內容獲取。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
方案可行性缺點
腳本阻擋插件❌內聯腳本無法攔截,破壞頁面功能
禁用 JS❌導致頁面無法渲染/交互
Chrome Extension + API Hook✅需開發擴展,但最徹底