Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

javascript - 为什么我的用户脚本仅在调试后才能工作?(Why my userscript is working only after debugging?)

I am trying to run an userscript in certain website.

(我正在尝试在某些网站上运行用户脚本。)

The objective is copy the information about each word searched in a on-line dictionary.

(目的是复制有关在线词典中搜索到的每个单词的信息。)

My browser is Chrome and I use the Tampermonkey extension.

(我的浏览器是Chrome,我使用的是Tampermonkey扩展程序。)

My code is not working, and I don't know what is the reason, since there is no errors appearing on console.

(我的代码无法正常工作,我也不知道是什么原因,因为控制台上没有出现错误。)

But If I stop the code execution by inserting debugger in my code and then just click on "Resume code execution", on Chrome Developer Tools, the code works, even though I don't do any change.

(但是,如果我通过在代码中插入debugger来停止代码执行,然后单击Chrome开发人员工具上的“恢复代码执行”,即使我不做任何更改,代码也可以正常工作。)

Why is that happening?

(为什么会这样呢?)

How can I fix that?

(我该如何解决?)

This is my code, along with the Tampermonkey standard userscript code:

(这是我的代码,以及Tampermonkey标准用户脚本代码:)

(function() {(function(context, fapply, console) {with (context) {(function(module) {"use strict";try {
fapply(module, context, [,,context.CDATA,context.uneval,context.define,context.module,context.exports,context.GM,context.GM_info]);} catch (e) {if (e.message && e.stack) {console.error("ERROR: Execution of script 'StudyFacilitator' failed! " + e.message);console.log(e.stack.replace(/(\(eval at )?<anonymous>[: ]?)|([s.]*at Object.tms_[sS.]*)/g, ""));} else {console.error(e);}}
})(function (context,fapply,CDATA,uneval,define,module,exports,GM,GM_info) {

// ==UserScript==
// @name         StudyFacilitator
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Copy automatically the info about each word searched.
// @author       Bruno M. B. Sdoukos
// @match        https://www.wordreference.com/enpt/*
// @grant        none
// ==/UserScript==

(function(){
    'use strict'

    // My code starts here

    let wordData = {word: '', meaning: '', translation: ''};
    wordData.word += document.querySelector('#articleHead > .headerWord').innerText + ' ' + document.querySelector('#pronWR').innerText;

    const tooltips = document.querySelectorAll('.tooltip');
    for (let i = 0; i < tooltips.length; i++) {
        tooltips[i].style.display = 'none';
    }

    let wordUnits = document.querySelectorAll('tr[id^="enpt:"]');
    let loopsNumber = wordUnits.length < 3 ? wordUnits.length : 3
    for (let i = 0; i < loopsNumber; i++) {
        wordData.meaning += wordUnits[i].querySelector('td:nth-child(2)').innerText + '; ';
        let firstTranslation = wordUnits[i].querySelector('.ToWrd');
        wordData.translation += firstTranslation.innerText + '; '
        let secondTranslation = wordUnits[i].nextElementSibling.querySelector('.ToWrd');
        if (secondTranslation !== null) {
            wordData.translation += secondTranslation.innerText + '; '
        }
    }
    wordData = wordData.word + ' | ' + wordData.meaning + ' | ' + wordData.translation;

    for (let i = 0; i < tooltips.length; i++) {
        tooltips[i].style.display = 'initial';
    }

    let textBox = document.createElement('textarea');
    document.querySelector('body').appendChild(textBox);
    let text = document.createTextNode(wordData);
    textBox.appendChild(text);
    textBox.select();
    document.execCommand('copy');
    document.querySelector('body').removeChild(textBox);

    // My code ends here

})()
})}})(this.context, this.fapply, this.console);
//# sourceURL=chrome-extension://dhdgffkkebhmkfjojejmpbldmpobfkfo/userscript.html?id=4cf965a9-a02e-49c2-b22d-fecc7d272440
}).apply(window["__u__3218613.1053551002_"])
  ask by Bruno Sdoukos translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...