我正在尝试编写一个 tampermonkey 脚本并想调用网页提供的 javascript 函数。页面的 HTML 如下所示:
<ui-button class="action-button-spacing" id="downloadAs" variant="normal" text="Download as" click="openDownloadAsPanel()" data-disabled="actionButtonsDisabled()" initialized="true"><button class="ui-button ui-button-size-normal ui-button-variant-normal ui-hover-child-icons" type="submit"><span region-container="text">Download as</span></button></ui-button>
我的脚本在页面上创建了一个按钮,我希望它调用 openDownloadAsPanel 方法,该方法的定义在 JS 文件中(不是 HTML 文件的一部分)。
为此,我尝试了这个:
function addFunction(func, exec) {
var script = document.createElement("script");
script.textContent = "-" + func + (exec ? "()" : "");
document.body.appendChild(script);
document.body.removeChild(script); // clean up
}
function myFunction () {
return openDownloadAsPanel();
}
然后点击我创建的按钮,addFunction(myFunction, true); 我收到一个错误:openDownloadAsPanel
未定义
此外,我不知道 javascript 文件的文件,因为它由 cloudfront 提供,并且如果文件更改,名称会不断更改。或者可能我将不得不解析写在 HTML 文件中的所有 javascript 文件名/路径。