我应该怎么做才能使它在greasemonkey中工作?:
Element.prototype._attachShadow = Element.prototype.attachShadow;
Element.prototype.attachShadow = function () {
return this._attachShadow( { mode: "open" } );
};
现在,当我使用这个时:
document.body.attachShadow({mode:"closed"}).innerHTML = "dec";
这是错误吗:
错误:访问对象的权限被拒绝
我应该怎么做才能使应用程序使用替代方法?
编辑(更多信息):
Firefox Nightly 62.0a1 (2018-06-18) (64 位)
Greasemonkey 4.4.0
// ==UserScript==
// @name Unnamed Script 124585
// @version 1
// @grant none
// @include http://kind-clarke-ced7cb.bitballoon.com/
// ==/UserScript==
Element.prototype._attachShadow = Element.prototype.attachShadow;
Element.prototype.attachShadow = function () {
return this._attachShadow( { mode: "open" } );
};
console.log("US work");
测试页面: http: //kind-clarke-ced7cb.bitballoon.com/
<div id="test" style="border: 1px solid silver;">xxx</div>
<div id="info">wait 4 sec...(so that UserScript can load itself before)</div>
<script>
setTimeout(()=>{
document.querySelector("#test").attachShadow({mode:"closed"}).innerHTML = "dec";
document.querySelector("#info").innerHTML += `<br/>run: document.querySelector("#test").attachShadow({mode:"closed"}).innerHTML = "dec";`;
}, 3000);
setTimeout(()=>{
document.querySelector("#info").innerHTML += `<br/>shadowRoot of #test is: ${(document.querySelector("#test").shadowRoot)}`;
}, 4000);
</script>
现在,attachShadow 方法的这种替换根本不起作用。
#test 的 shadowRoot 返回 null 但应该是[object ShadowRoot]
,因为 UserScript 强制它。