获取 Tampermonkey 中跨度文本的值

编程入门 行业动态 更新时间:2024-10-25 16:30:09
本文介绍了获取 Tampermonkey 中跨度文本的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在 JS 中使用 Tampermonkey 脚本.我有一个网站,以 UTC 格式显示用户的位置和时区.

I am playing with Tampermonkey scripts in JS. I have a website that shows the user's location and timezone in UTC format.

<span class="optional-wrapper">
   <span class="UTCText"> ==$0
   <!-- react-text: 60 --> 
   " (UTC "
   <!-- /react-text -->
   <!-- react-text: 61 -->
   "+10:00" 
   <!-- /react-text -->
   <!-- react-text: 62 -->
   ") "
   <!-- /react-text -->
   </span>
</span>

我想读取 UTC 时区 (UTC +10:00) 并将其转换为时间.我试过这样的事情,但它不起作用.有人可以指出我可以了解的正确方向吗?

I want to read the UTC timezone (UTC +10:00) and convert it into a time. I tried something like this but it doesn't work. Can someone point me in the right direction where i can learn about this?

function getTimeZone(UTCText){
document.getElementsByClassName(UTCText);
console.log(UTCText)
}

目前我只想打印到控制台,所以我知道我正在正确读取时区.

At the moment I just want to print to the console so I know I am reading the timezone correctly.

推荐答案

要从 static 页面获取文本,请使用 .textContent 然后解析要提取的字符串您想要的价值.
这是一个演示:

To get the text from a static page, use .textContent and then parse the string to extract the value you want.
Here's a demo:

var tzOffset    = "Not found!";
var tzNode      = document.querySelector (".UTCText");
if (tzNode) {
    let ndTxt   = tzNode.textContent;
    let tzMtch  = ndTxt.match (/(-?\d+:\d+)/);
    if (tzMtch  &&  tzMtch.length > 1) {
        tzOffset = tzMtch[1];
    }
}
console.log ("Found timezone offset: ", tzOffset);

<span class="optional-wrapper">
   <span class="UTCText"> ==$0
   <!-- react-text: 60 -->
   " (UTC "
   <!-- /react-text -->
   <!-- react-text: 61 -->
   "-10:30"
   <!-- /react-text -->
   <!-- react-text: 62 -->
   ") "
   <!-- /react-text -->
   </span>
</span>


但是,该页面似乎使用了 reactjs,这意味着它是 AJAX 驱动的.


However, that page looks to be using reactjs, which means it's AJAX-driven.

对于使用 AJAX 的页面,您通常必须使用 AJAX 感知技术,例如 waitForKeyElements.以下是完整的 Tampermonkey 用户脚本中的内容:

For AJAX'd pages, you often have to use AJAX-aware techniques such as waitForKeyElements. Here's what that looks like in a complete Tampermonkey userscript:

// ==UserScript==
// @name     _Get timezone offset text from a span
// @match    *://YOUR_SERVER.COM/YOUR_PATH/*
// @noframes
// @require  https://ajax.googleapis/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// @grant    GM.getValue
// ==/UserScript==
// @grant    none
//- The @grant directives are needed to restore the proper sandbox.
/* global $, waitForKeyElements */
/* eslint-disable no-multi-spaces, curly */

waitForKeyElements (".UTCText", getTZ_Offset);

function getTZ_Offset (jNode) {
    var tzOffset    = "Not found!";
    let ndTxt       = jNode.text ();
    let tzMtch      = ndTxt.match (/(-?\d+:\d+)/);
    if (tzMtch  &&  tzMtch.length > 1) {
        tzOffset    = tzMtch[1];
    }
    console.log ("Found timezone offset: ", tzOffset);
}

这篇关于获取 Tampermonkey 中跨度文本的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 10:11:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1407307.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跨度   文本   Tampermonkey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!