在 Tampermonkey 中获取跨度文本的值
Get value of span text in Tampermonkey
我正在玩 JS 中的 Tampermonkey 脚本。我有一个以 UTC 格式显示用户位置和时区的网站。
<span class="optional-wrapper">
<span class="UTCText"> ==[=10=]
<!-- react-text: 60 -->
" (UTC "
<!-- /react-text -->
<!-- react-text: 61 -->
"+10:00"
<!-- /react-text -->
<!-- react-text: 62 -->
") "
<!-- /react-text -->
</span>
</span>
我想读取 UTC 时区 (UTC +10:00) 并将其转换为时间。我试过这样的东西,但它不起作用。有人可以指出我可以了解的正确方向吗?
function getTimeZone(UTCText){
document.getElementsByClassName(UTCText);
console.log(UTCText)
}
目前我只想打印到控制台,这样我就知道我正在正确读取时区。
要从 static 页面获取文本,请使用 .textContent
然后解析字符串以提取所需的值。
这是一个演示:
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"> ==[=11=]
<!-- react-text: 60 -->
" (UTC "
<!-- /react-text -->
<!-- react-text: 61 -->
"-10:30"
<!-- /react-text -->
<!-- react-text: 62 -->
") "
<!-- /react-text -->
</span>
</span>
但是,该页面看起来正在使用 reactjs,这意味着它是 AJAX-driven。
对于 AJAX 的页面,您通常必须使用 AJAX-aware 技术,例如 waitForKeyElements。这是 完整的 Tampermonkey 用户脚本中的样子:
// ==UserScript==
// @name _Get timezone offset text from a span
// @match *://YOUR_SERVER.COM/YOUR_PATH/*
// @noframes
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/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);
}
我正在玩 JS 中的 Tampermonkey 脚本。我有一个以 UTC 格式显示用户位置和时区的网站。
<span class="optional-wrapper">
<span class="UTCText"> ==[=10=]
<!-- react-text: 60 -->
" (UTC "
<!-- /react-text -->
<!-- react-text: 61 -->
"+10:00"
<!-- /react-text -->
<!-- react-text: 62 -->
") "
<!-- /react-text -->
</span>
</span>
我想读取 UTC 时区 (UTC +10:00) 并将其转换为时间。我试过这样的东西,但它不起作用。有人可以指出我可以了解的正确方向吗?
function getTimeZone(UTCText){
document.getElementsByClassName(UTCText);
console.log(UTCText)
}
目前我只想打印到控制台,这样我就知道我正在正确读取时区。
要从 static 页面获取文本,请使用 .textContent
然后解析字符串以提取所需的值。
这是一个演示:
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"> ==[=11=]
<!-- react-text: 60 -->
" (UTC "
<!-- /react-text -->
<!-- react-text: 61 -->
"-10:30"
<!-- /react-text -->
<!-- react-text: 62 -->
") "
<!-- /react-text -->
</span>
</span>
但是,该页面看起来正在使用 reactjs,这意味着它是 AJAX-driven。
对于 AJAX 的页面,您通常必须使用 AJAX-aware 技术,例如 waitForKeyElements。这是 完整的 Tampermonkey 用户脚本中的样子:
// ==UserScript==
// @name _Get timezone offset text from a span
// @match *://YOUR_SERVER.COM/YOUR_PATH/*
// @noframes
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/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);
}