首页 > 解决方案 > 使用 Inspect Element 更改 FireFox 中的 javascript 代码

问题描述

这是javascript代码:

<script type="text/javascript">
    var javascript_countdown = function () 
    {
        var time_left = 10; //number of seconds for countdown
        var keep_counting = 1;
        var no_time_left_message = 'Please Activate Javascript.';
        function countdown() 
        {
            if(time_left < 2) 
            {
                keep_counting = 0;
            }
            time_left = time_left - 1;
        }
        function add_leading_zero( n ) 
        {
            if(n.toString().length < 2) 
            {
                return '0' + n;
            } 
            else 
            {
                return n;
            }
        }
        function format_output() 
        {
            var hours, minutes, seconds;
            seconds = time_left % 60;
            minutes = Math.floor(time_left / 60) % 60;
            hours = Math.floor(time_left / 3600);
            seconds = add_leading_zero( seconds );
            minutes = add_leading_zero( minutes );
            hours = add_leading_zero( hours );
            return  minutes + ':' + seconds;
        }
        function show_time_left() 
        {
            document.getElementById('javascript_countdown_time').innerHTML = format_output();//time_left;
            document.getElementById('javascript_countdown_time_bottom').innerHTML = format_output();//time_left;
        }
        function no_time_left() 
        {
            //document.getElementById('javascript_countdown_time').innerHTML = no_time_left_message;
            //alert("");
            document.FormAzmoon.submit();
        }
        return {
            count: function () {
                countdown();
                show_time_left();
            },
            timer: function () {
                javascript_countdown.count();
                if(keep_counting) {
                    setTimeout("javascript_countdown.timer();", 1000);
                } else {
                    no_time_left();
                }
            },
            init: function (n) {
                time_left = n;
                javascript_countdown.timer();
            }
        };
    }();
    javascript_countdown.init(601);// Time Recorder Secends
</script>

此 javascrip 代码是页面中的反向计时器(10 分钟)。
当 timer = 0 时,它会自动提交页面。
我想操纵它以增加时间。
我认为增加时间的最佳方法是这条线:

setTimeout("javascript_countdown.timer();", 1000);

改成 :

setTimeout("javascript_countdown.timer();", 100000);

当我在 Firefox 中更改此值时,没有任何反应Inspector。 那么解决方案是什么? 如果除了firefox还有其他方法请教我! 还教我如何通过控制台重写和操作函数内的变量!Inspect Element


标签: javascriptfirefoxinspect-element

解决方案


JS will get parsed and run as soon as it's in the DOM. Changing the page markup after the JS has run will not change the running of the JS. You'll need to change it some other way.

One option would be to overwrite the .timer function with your own function. After the page script has run, open your console and run:

// save a reference to the original function if you need to
const origTimer = javascript_countdown.timer;
javascript_countdown.timer = () => null;

This will prevent the page's calls to .timer from doing anything. Then, once you want to submit the form, just call document.FormAzmoon.submit(); manually.

Also teach me how can i rewrite and manipulate a variable inside a function via console!

If you actually had to resort to that approach, it's just barely possible, see this question for details. In short, you'd need to use a userscript (or something else that runs at the beginning of pageload), use a MutationObserver to intercept the addition of the <script> tag, and fiddle with its textContent once you detect the addition of the script.


推荐阅读