【问题标题】:Call a function in Javascript file from tampermonkey script从 tampermonkey 脚本调用 Javascript 文件中的函数
【发布时间】:2018-12-07 02:26:36
【问题描述】:

我正在尝试编写一个 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 文件名/路径。

【问题讨论】:

标签: javascript tampermonkey greasemonkey-4


【解决方案1】:

我认为您不能调用网页运行的函数。您最好的选择可能是尝试查看函数中的代码是什么,并使用相同的代码定义您自己的函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-17
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 2017-12-02
    • 2014-02-14
    • 2015-11-13
    相关资源
    最近更新 更多