我正在构建一个Chrome扩展程序,它将为Trello添加其他功能。
我面临的问题是Trello某种程度上禁用了html5拖放,因此当我拖动具有属性draggable="true"
的某些DOM(我用扩展名注入)时,不会出现可拖动元素的半透明表示。
..
(拖动红色框,它<div draggable="true" style="..."></div>
不会显示半透明图像)
..
通过使用Chrome DevTool检查页面,我看到有一个dragstart
设置为的事件监听器document
。
..
If I remove the dragstart
event listener (by clicking the "Remove" button next to it inside the DevTool's Event Listeners window) then the drag works correctly as shown in the screenshot below. (The red box on the right is the translucent representation created by the html5 drag mechanism).
..
So my question is: is there any way to override that dragstart
event listener with a chrome extension for the elements that I intend to make draggable?
P.S. I am plaining to use react DnD eventually which is based on the html5 drag mechanism. And apparently, I am using react to build my extension :)
..
Edited: Here are the code from Trello where the dragstart
event listener is attached.
n(document).bind("dragstart", function(e) {
return n(e.target).closest(".ui-draggable, .js-draggable").length > 0
}),
The above code is placed within n(document).ready(function() { /* HERE */ }
and n = e("jquery")
. (Not 100% sure but seems that n
is just jQuery.)
Since the event listener is attached anonymously, it is not possible to remove it afterwards (Correct me if I am wrong).
To begin with, a reminder: JavaScript defined in the page lives in a separate JS context from content scripts (the "isolated world" principle). Read up on that if unfamiliar.
With that in mind, you have two potential approaches:
You can be the fastest gun in the West and attach your listener first, before any of the page's code has a chance to execute, with a run_at: "document_start"
content script. From there, you can cancel event propagation so that the page's listener won't receive it.
您可以通过注入页面上下文来自己删除侦听器。当然,如何删除它取决于它的连接方式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句