我对 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?
The web page is generated by JS. This parameter prevents me from selecting text
发布评论
评论(1)
返回
true
不会有帮助,这个值在 DOM 事件中不会做任何事情。它在控制台中工作,因为您的侦听器只是替换了页面的侦听器,但它在内容脚本中不起作用,因为它们的 JS 代码和对象在设计上与页面隔离。一种解决方案是将该脚本元素中的代码添加到页面,如此答案中所述,但在页面上下文中运行代码是由于 Chrome 扩展程序和 Firefox WebExtensions 实现中均存在错误,因此并非 100% 安全(
wrappedJSObject
也会受到影响)。这里有一个更简单的解决方案:阻止页面看到该事件。
一些解释。 DOM 事件分两个阶段进行调度:1) 捕获阶段,事件从
window
传播到document
到到
>
并向下到达触发事件的元素,以及 2) 冒泡阶段,其中事件从目标元素向上冒泡,通过 DOM 树层次结构到达window.
事件侦听器的默认阶段是“冒泡”,因此最简单的解决方案是在“捕获”阶段注册侦听器,在该阶段它将停止事件的“传播”以防止后续侦听器看到该事件。我们在
window
上执行此操作,它是此阶段的第一个接收者,因此我们的侦听器是第一个。有些网站可以通过使用相同的代码并首先运行它来规避它,这很容易发生,因为默认情况下内容脚本在 DOMContentLoaded 之后运行。为了解决这个问题,您可以使用
"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.
Some explanation. DOM events are dispatched in two phases: 1) the capturing phase where the event travels from
window
todocument
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 towindow
.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.