我正在关注的这个推特帐户会在不同时区的军事时间发布推文。我正在尝试使用Firefox中的GreaseMonkey Addon将javascript更改为适当的时区并定期更改。问题是我似乎无法让它发挥作用。
我关注的帐户: https://twitter.com/pso2_emg_bot
我使用的脚本:
// ==UserScript==
// @name PSO2 Emg Bot Script
// @namespace Twitter
// @description Convert time to EST
// @include https://twitter.com/*
// @version 1
// ==/UserScript==
function replaceText(){
var theDiv = document.getElementsByClassName("js-tweet-text");
var theText = theDiv .innerHTML;
// Replace words
theText = theText.replace("4:00~4:30", "2:00 P.M. ~ 2:30 P.M.");
theText = theText.replace("14:00~14:30", "12:00 A.M. ~ 12:30 A.M.");
theText = theText.replace("15:00~15:30", "1:00 A.M. ~ 1:30 A.M.");
theText = theText.replace("16:00~16:30", "2:00 A.M. ~ 2:30 A.M.");
theDiv.innerHTML = theText;
}
目前还不完整,因为我只需要将所有时间都包含在脚本中进行搜索,然后才能让它工作。如果有人能告诉我我做错了什么以及如何解决它,我会非常感激。
答案 0 :(得分:2)
function replaceText()
,但有
脚本中没有任何东西可以调用该函数,所以没有
执行。theDiv
实际上会包含一个div数组,所以你不能这样设置或设置它的innerHTML。theDiv .innerHTML;
Twitter已经加载了jQuery,所以在你的情况下使用它而不是vanilla JS可能更容易。
//...
// ==/UserScript==
var $ = unsafeWindow.$;
var theDivs = $(".js-tweet-text");
theDivs.each(function(){
var theText = $(this).text();
theText = theText.replace("7:00~7:30", "7:00 P.M. ~ 7:30 P.M.");
//other replacements you want to make.
//consider using a regular expression instead of one line for each hour.
$(this).text(theText);
});
答案 1 :(得分:1)
有几件事:
innerHTML
。它会破坏事物(加上它变慢)。使用jQuery或“DOM技术”(也如下所示)。.replace()
语句。总而言之,一个完整的工作脚本:
// ==UserScript==
// @name _PSO2 Emg Bot Script
// @namespace Twitter
// @description Convert time to EST
// @include https://twitter.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
waitForKeyElements ("p.js-tweet-text", ChangeSpecialTimeStrs);
function ChangeSpecialTimeStrs (jNode) {
var node = jNode[0];
//-- Search only in the first-level text nodes of this paragraph.
for (var K = 0, numC = node.childNodes.length; K < numC; ++K) {
var childNode = node.childNodes[K];
if (childNode.nodeType === Node.TEXT_NODE) {
if (childNode.nodeValue.length > 8) {
//-- Anything shorter can't have our kind of string.
childNode.nodeValue = childNode.nodeValue.replace (
/*-- This matches strings like: "5:00~15:30"
Where "~" may be unicode FF5E
*/
/\b(\d{1,2}):(\d{2})(?:~|\uFF5E)(\d{1,2}):(\d{2})\b/gi,
shiftHourStr
);
}
}
}
}
function shiftHourStr (
matchedStr, //- Housekeeping supplied by .replace()
hour1Str, minute1Str, //- Payload vals from () groups
hour2Str, minute2Str, //- Payload vals from () groups
matchOffset, totalString //- Housekeeping supplied by .replace()
) {
//-- Return a string with a format like: "12:00 A.M. ~ 12:30 A.M."
const tzOffsetHours = 10;
var newHr1Arry = getHourOffset (hour1Str, tzOffsetHours);
var newHr2Arry = getHourOffset (hour2Str, tzOffsetHours);
var outputStr = newHr1Arry[0] //-- Hour value
+ ":" + minute1Str
+ newHr1Arry[1] //-- AM or PM
+ " ~ "
+ newHr2Arry[0] //-- Hour value
+ ":" + minute2Str
+ newHr2Arry[1] //-- AM or PM
;
return outputStr;
};
function getHourOffset (hourVal, hoursOffset) {
var amPmStr = "A.M.";
var newHourVal = parseInt (hourVal, 10) + hoursOffset;
if (newHourVal > 23) {
newHourVal -= 24;
}
if (newHourVal >= 12) {
newHourVal -= 12;
amPmStr = "P.M.";
}
if (newHourVal == 0) {
newHourVal = 12;
amPmStr = "A.M.";
}
return [newHourVal, " " + amPmStr];
}