我最简单的chrome扩展没有生效

发布于 2025-01-09 01:53:38 字数 1212 浏览 0 评论 0 原文

我对 JavaScript 不熟悉。

我自己写了一个扩展,但是没有用。

扩展功能很简单,就是激活onselectstart参数,因为网页中的这个参数导致无法选中网页的文本然后进行复制。如果激活此参数,我可以选择并复制。

我可以直接在chrome控制台执行这个JavaScript命令,并启用onselectstart,效果很好:

document.body.onselectstart = function(){return true;}; 

但是放入扩展程序后不会生效。

我的扩展很简单,就两个文件:

manifest.json:

{
  "manifest_version": 2,
  "name": "Copy unblocker",
  "version": "1.0",
  "description": "enable onselectstart",
  "icons": { "16": "images/logo.png" },  

  "permissions": [
    "activeTab"
  ],
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["js/content_script.js"]
    }
  ]
}

content_ script.js:

document.body.onselectstart= function(){
  return true;
}

怎么了,或者能给我一个chrome扩展脚本,可以改变<代码>onselectstart参数?

文件树

网页由 JS 生成。该参数阻止我选择文本

I'm not familiar with JavaScript.

I wrote an extension myself, but it didn't work.

The extended function is very simple, that is to activate the onselectstart parameter, because this parameter in the web page makes it impossible to select the text of the web page and then copy it. If I activate this parameter, I can select and copy.

I can execute this JavaScript command directly in the chrome console, and enable the onselectstart, works well:

document.body.onselectstart = function(){return true;}; 

but it will not take effect after put it into the extension.

My extension is very simple, just two files:

manifest.json:

{
  "manifest_version": 2,
  "name": "Copy unblocker",
  "version": "1.0",
  "description": "enable onselectstart",
  "icons": { "16": "images/logo.png" },  

  "permissions": [
    "activeTab"
  ],
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["js/content_script.js"]
    }
  ]
}

content_ script.js:

document.body.onselectstart= function(){
  return true;
}

What's wrong, or can you give me an chrome extension script that can change the onselectstart parameter?

file trees

enter image description here

The web page is generated by JS. This parameter prevents me from selecting text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2025-01-16 01:53:38

返回 true 不会有帮助,这个值在 DOM 事件中不会做任何事情。它在控制台中工作,因为您的侦听器只是替换了页面的侦听器,但它在内容脚本中不起作用,因为它们的 JS 代码和对象在设计上与页面隔离。

一种解决方案是将该脚本元素中的代码添加到页面,如此答案中所述,但在页面上下文中运行代码是由于 Chrome 扩展程序和 Firefox WebExtensions 实现中均存在错误,因此并非 100% 安全(wrappedJSObject 也会受到影响)。

这里有一个更简单的解决方案:阻止页面看到该事件。

// Specifying `true` to register a capturing phase listener
window.addEventListener('selectstart', e => e.stopPropagation(), true);

一些解释。 DOM 事件分两个阶段进行调度:1) 捕获阶段,事件从 window 传播到 document > 并向下到达触发事件的元素,以及 2) 冒泡阶段,其中事件从目标元素向上冒泡,通过 DOM 树层次结构到达 window.

事件侦听器的默认阶段是“冒泡”,因此最简单的解决方案是在“捕获”阶段注册侦听器,在该阶段它将停止事件的“传播”以防止后续侦听器看到该事件。我们在 window 上执行此操作,它是此阶段的第一个接收者,因此我们的侦听器是第一个。

有些网站可以通过使用相同的代码并首先运行它来规避它,这很容易发生,因为默认情况下内容脚本在 DOMContentLoaded 之后运行。为了解决这个问题,您可以使用 "run_at": "document_start" 声明您的内容脚本,以便它在页面运行其脚本之前运行。

  "content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["js/content_script.js"],
    "run_at": "document_start"
  }]
}

Returning true won't help, this value doesn't do anything in DOM events. It worked in console because your listener simply replaced the listener of the page, but it doesn't work in content scripts as their JS code and objects are isolated form the page by design.

One solution is to add that code in a script element to the page as explained in this answer, but running code in the page context is not 100% safe due to a bug in both Chrome extensions and Firefox WebExtensions implementations (wrappedJSObject is also affected).

Here's a simpler solution: preventing the page from seeing the event.

// Specifying `true` to register a capturing phase listener
window.addEventListener('selectstart', e => e.stopPropagation(), true);

Some explanation. DOM events are dispatched in two phases: 1) the capturing phase where the event travels from window to document to <html> to <body> and down to the element that triggered the event, and 2) the bubbling phase where the event bubbles up from the target element all the way upwards through the DOM tree hierarchy to window.

The default phase for event listeners is "bubbling", so the easiest solution is to register your listener in the "capturing" phase where it will stop the event's "propagation" to prevent subsequent listeners from seeing the event. We did it on window, which is the first recipient in this phase, so our listener is the first one.

Some sites can circumvent it by using the same code and running it first, which can easily happen because content scripts run after DOMContentLoaded by default. To fight that you can declare your content script with "run_at": "document_start" so it runs before the page has run its scripts.

  "content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["js/content_script.js"],
    "run_at": "document_start"
  }]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文