Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Tampermonkey脚本不';如果标签位于后台,则无法加载(Firefox)_Javascript_Tampermonkey - Fatal编程技术网

Javascript Tampermonkey脚本不';如果标签位于后台,则无法加载(Firefox)

Javascript Tampermonkey脚本不';如果标签位于后台,则无法加载(Firefox),javascript,tampermonkey,Javascript,Tampermonkey,我已经创建了一个Tampermonkey脚本,它在网页中插入一个按钮,并在您单击它时下载一个文本文件。一切正常。但是,如果选项卡在后台加载(例如,您打开一个新选项卡而不跳到它),则按钮不会出现 两者之间没有区别 // @run-at document-idle 及 但是如果我把按钮注入一个静态元素 document.body.appendChild(button) 而不是 document.getElementsByClassName("sc-181ts2x-0")[0].app

我已经创建了一个Tampermonkey脚本,它在网页中插入一个按钮,并在您单击它时下载一个文本文件。一切正常。但是,如果选项卡在后台加载(例如,您打开一个新选项卡而不跳到它),则按钮不会出现

两者之间没有区别

// @run-at       document-idle

但是如果我把按钮注入一个静态元素

document.body.appendChild(button)
而不是

document.getElementsByClassName("sc-181ts2x-0")[0].appendChild(button)
它在后台100%工作

我假设这是因为我试图添加按钮的元素在加载时不存在,但这只发生在选项卡位于后台且

// @run-at       document-idle
应该确保它只在加载所有内容后运行

// @name         Pixiv Description Downloader
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Downloads captions/descriptions from pixiv to a work_title(pixiv_id).txt. The format is the same as that of PXdownloader, which does not support captions.
// @author       UglyGoblin
// @include      https://www.pixiv.net/member_illust.php?*
// @grant        none
// @require      https://raw.githubusercontent.com/eligrey/FileSaver.js/master/dist/FileSaver.js
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

     window.addEventListener('load', () => {
    addButton('\<b\>↓ Description \<\/b\>', downloadDescriptionFN)
    })
//insert button
    function addButton(text, onclick, cssObj) {
        cssObj = cssObj || {position: 'relative', right:'10px', top:'2px','z-index': 3}
        let button = document.createElement('button'), btnStyle = button.style
//add button next to PXdownloader button
        document.getElementsByClassName("sc-181ts2x-0")[0].appendChild(button)
        button.innerHTML = text
        button.onclick = onclick
//make button background transparent
        button.style.background = "none";
        button.style.border = "none";
        Object.keys(cssObj).forEach(key => btnStyle[key] = cssObj[key])
        return button
    }
//actual function to download on button press
    function downloadDescriptionFN() {
         //get url of current tab
         var url = window.location.href;
         //get the pixiv id from url
         var pixivID = url.split('illust_id=')[1];
         //get title of artwork
         var title = document.getElementsByClassName("sc-1u8nu73-3 igWZKS")[0].textContent;
         //add pixiv it to title => title(pixiv_id).txt
         var textfile = title.concat('\(',pixivID,'\).txt');
         //get captions as html
         var rawHTML = document.getElementsByClassName("_3BeBvbu");
         //replace <br> with \n
         var textWithLineBreaks = rawHTML[0].innerHTML.replace(/\<br\>/gi,"\n");
         //add title as title to textfile (for some reason needs 2 linebreaks to work)
         var finalTXT = title.concat("\n","\n",textWithLineBreaks);
         //create new blob with content of captions
         var blob = new Blob([finalTXT], {type: "text/plain;charset=utf-8"})
         //save the blob as textfile with the name title(pixiv).txt
         saveAs(blob, textfile)

    }
})();

/@name Pixiv Description Downloader
//@名称空间http://tampermonkey.net/
//@version 0.5
//@description将标题/描述从pixiv下载到作品标题(pixiv_id).txt。格式与PXdownloader相同,不支持字幕。
//@author UglyGoblin
//@包括https://www.pixiv.net/member_illust.php?*
//@grant none
//@需要https://raw.githubusercontent.com/eligrey/FileSaver.js/master/dist/FileSaver.js
//@在文档空闲时运行
//==/UserScript==
(功能(){
"严格使用",;
window.addEventListener('load',()=>{
添加按钮('\↓ 说明\',下载说明fn)
})
//插入按钮
功能添加按钮(文本、onclick、cssObj){
cssObj=cssObj |{位置:'相对',右:'10px',顶部:'2px','z-index':3}
让button=document.createElement('button'),btnStyle=button.style
//在PXdownloader按钮旁边添加按钮
document.getElementsByClassName(“sc-181ts2x-0”)[0]。appendChild(按钮)
button.innerHTML=文本
button.onclick=onclick
//使按钮背景透明
button.style.background=“无”;
button.style.border=“无”;
Object.keys(cssObj).forEach(key=>btnStyle[key]=cssObj[key])
返回按钮
}
//按下按钮时下载的实际功能
函数下载描述fn(){
//获取当前选项卡的url
var url=window.location.href;
//从url获取pixiv id
var pixivID=url.split('illust_id=')[1];
//获得作品名称
var title=document.getElementsByClassName(“sc-1u8nu73-3 igWZKS”)[0].textContent;
//将pixiv it添加到title=>title(pixiv_id).txt
var textfile=title.concat(“\(”,pixivID,“\).txt”);
//以html格式获取标题
var rawHTML=document.getElementsByClassName(“'u 3BeBvbu”);
//将
替换为\n var textWithLineBreaks=rawHTML[0]。innerHTML.replace(/\/gi,“\n”); //将标题作为标题添加到文本文件(由于某些原因,需要两个换行符才能工作) var finalTXT=title.concat(“\n”,“\n”,textWithLineBreaks); //创建包含标题内容的新blob var blob=new blob([finalTXT],{type:“text/plain;charset=utf-8”}) //将blob另存为名为title(pixiv.txt)的文本文件 另存为(blob,textfile) } })();
解决方案是使用jQuery和WaitForkElements并替换

window.addEventListener('load',

现在,每次都会成功加载该按钮

window.addEventListener('load',
waitForKeyElements(".sc-181ts2x-0",