用JS前缀获取对象后再更新对象内容

// 存储是否已设置过日期的标志
let isDateSet = false;

// 每秒检测目标元素是否存在
const checkElementInterval = setInterval(() => {
  // 若已设置过日期,不再进行检测
  if (isDateSet) {
    clearInterval(checkElementInterval);
    return;
  }

  // 选择目标元素
  const targetElement = document.querySelector('div[class^="shipping-container"] > div[class^="shipping-address-time"] > strong');
  
  if (targetElement) {
    // 标记已设置过日期
    isDateSet = true;
    // 清除定时器,停止检测
    clearInterval(checkElementInterval);
    
    // 获取当前日期和时间
    const currentDate = new Date();
    const currentHour = currentDate.getHours();
    
    // 根据当前时间决定增加的天数(下午2点前加2天,否则加3天)
    const daysToAdd = currentHour < 14 ? 2 : 3;
    currentDate.setDate(currentDate.getDate() + daysToAdd);
    
    // 定义星期和月份的缩写
    const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    
    // 构建格式化的日期字符串
    const formattedDate = `${days[currentDate.getDay()]}. ${months[currentDate.getMonth()]}. ${currentDate.getDate()}`;
    
    // 更新元素内容
    targetElement.innerHTML = formattedDate;
  }
}, 1000);

个人微信:ssevening

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注