【发布时间】:2019-07-22 01:14:48
【问题描述】:
我正在尝试绕过我目前正在学习的课程中的计时器,除非计时器已达到 0,否则我无法前进到下一页。我正在使用 TamperMonkey 将函数的值设置为值将允许我让计时器达到 0 但无济于事。如何通过控制台或 tampermonkey 将参数传递给 Javascript 中的函数,以便将计时器设置为 0 并在每个页面中前进?我主要用 Python 编程,这是我第一次尝试用 Javascript 编码。
使用最新版本的 Google Chrome 和 Tampermonkey。我当前的 Tampermonkey 脚本使计时器无效并将其设置为空值 (----)
(function() {
'use strict';
// Your code here...
window.seconds = 0;
var scriptContent = "UpdateTimer('TimeRemainingClock', 0, 'DOWN')";
var script = document.createElement('script')
script.appendChild(document.createTextNode(scriptContent));
document.getElementsByTagName('head')[0].appendChild(script);
})();
什么都没有。
网站的JS代码是:
var pageLoaded = 0;
var timerStatus = 'pending';
var secondsRemaining = -1;
var secondsElapsed = -1;
var startTicks = 0;
var errorCount = 0;
var estimatedSecondsRemaining = -1;
var zeroTimeCounter = 0;
var intervalIdUpdateBothTimers;
var nonLinearGuid = null;
function UpdateElapsedTimer()
{
var s = secondsElapsed + (GetTickDiff()/1000);
UpdateTimer('TotalTimeClock', s, 'UP');
}
function GetTickDiff()
{
var d = new Date();
var tickDiff = d.getTime() - startTicks;
return tickDiff;
}
function UpdateRemainingTimer()
{
var s = secondsRemaining - (GetTickDiff()/1000);
estimatedSecondsRemaining = s;
if (s < 0) s = 0;
UpdateTimer('TimeRemainingClock', s, 'DOWN');
}
function UpdateTimer(ClockID,ElapsedSeconds,ClockDirection){
//check to see if we can run this code yet
if(document.getElementById && document.getElementById(ClockID) != null){
//declare vars
var _Seconds = 0;
var _Minutes = 0;
var _Hours = 0;
//Format Seconds
_Seconds = Math.floor(ElapsedSeconds % 60);
if(_Seconds <= 9) {
_Seconds = "0"+_Seconds;
}
//Format minutes
_Minutes = Math.floor(ElapsedSeconds/60 % 60);
if(_Minutes <= 9) {
_Minutes = "0"+_Minutes;
}
//Format hours
_Hours = Math.floor(ElapsedSeconds/3600);
if(_Hours <= 9){
_Hours = "0"+_Hours;
}
document.getElementById(ClockID).innerHTML = _Hours + ":" + _Minutes + ":" + _Seconds;
if (timerStatus != 'active')
{
setTimeout('UpdateTimer(\''+ClockID+'\','+ElapsedSeconds+',\''+ClockDirection+'\')',1000);
return;
}
if(ElapsedSeconds > 0 || ClockDirection == "UP"){
if(ClockDirection == "UP")
{
ElapsedSeconds = ElapsedSeconds + 1;
}
else
{
ElapsedSeconds = ElapsedSeconds - 1;
}
//setTimeout('UpdateTimer(\''+ClockID+'\','+ElapsedSeconds+',\''+ClockDirection+'\')',1000);
}
else{
//Timer has hit zero. Lets make sure the next buttons are visible.
$('#next_top').show();
$('#next_bot').show();
}
}
else if(!pageLoaded) //call function again in 100ms
{
//setTimeout('UpdateTimer(\''+ClockID+'\','+ElapsedSeconds+',\''+ClockDirection+'\')',100);
}
使用 TamperMonkey 脚本时,输出为: TimerUtils.01.js:262 未捕获类型错误:UpdateTimer 不是函数 在 UpdateElapsedTimer (VM27051 TimerUtils.01.js:262) 在 UpdateBothTimers (VM27051 TimerUtils.01.js:200) 在:1:1
我了解范围的概念,但不了解我的输出应该是什么。
感谢任何帮助。非常感谢!
【问题讨论】:
-
在我看来 UpdateTimer 为空或在调用 UpdateElapsedTime 之前被重新定义为另一个值。您是在 UpdateElapsedTimer 之前调用 UpdateTimer 吗?如果您乱序调用,也许有一些代码会重新定义 UpdateTimer。我认为答案在 TimerUtils.01.js 中,你能分享一下吗?
标签: javascript console