首先,如果我滥用任何条款,我想道歉。我刚刚学习了Javascript,它是非常基础的知识。我希望能够做得更好。我一直在试图弄清楚如何将.json文件中的一些文本加载到脚本中。我正在使用广播程序Xsplit,它可以选择从文件加载文本。我是电子竞技流媒体导演和制作人,我使用一个特殊的程序,创建两个文件(XML和JSON),其中包含我制作的锦标赛的值。用于加载代码的脚本位于:
/*
This loads text from [FILEPATH] in between [DELIMITER_1] and [DELIMITER_2].
If [DELIMITER_1] and [DELIMITER_2] is not found, Title shows the whole text.
An optional [UPDATEINTERVAL] in milliseconds may also be set to auto-update.
The script will check the local file for any changes in text every [UPDATEINTERVAL] milliseconds
which will automatically update the text within the stage.
Setting it to 0 or lower disables auto-update.
*/
/**
* @name FILEPATH
* @label File Path
* @type file
* @filters "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"
* @description The path of the file from where the script will extract text
*/
var FILEPATH = "C:\\Stream Control\\streamcontrol.xml";
/**
* @name DELIMETER_1
* @label Delimeter 1
* @type text
* @description The text, which when found signals the start of extracting text. If not found, script will show the whole text.
*/
var DELIMETER_1 = "<pName1>";
/**
* @name DELIMETER_2
* @label Delimeter 2
* @type text
* @description The text, which when found signals the end of extracting text. If not found, script will show the whole text.
*/
var DELIMETER_2 = "</pName1>";
/**
* @name UPDATEINTERVAL
* @label Update Interval
* @type int
* @positiveOnly true
* @description Optional. If set, the script will check the local file for any changes in text every X seconds, which will automatically update the text within the stage.
*/
var UPDATEINTERVAL = 1;
/**
* @name LINEBREAK_BEHAVIOR
* @label Behavior on Line Breaks
* @type select
* @options Preserve Line Breaks||Ignore Line Breaks||Replace
* @description Preserve Line Breaks = Line breaks will be preserved, and text will be wrapped to the next line||Ignore Line Breaks = Line breaks will simply be omitted, and without spacing. Text will be displayed on single line||Replace = Replace line breaks with a specific character or set of characters
*/
var LINEBREAK_BEHAVIOR = "Replace";
/**
* @name REPLACE_WITH
* @label Replace Line Break With
* @type text
* @description The text to replace line breaks with, if Replace is selected as line break behavior
*/
var REPLACE_WITH = " ";
/*Do not modify anything below*/
/**
* @name XJS_URL
* @description XJS FRAMEWORK URL LOCATION
*/
var XJS_URL = "http://cdn2.xsplit.com/xjs/download/1.4.1/xjs.min.js?source";
function loadScript(url, callback)
{
// Adding the script tag to the head
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// Then bind the event to the callback function.
// There are several events for cross browser compatibility.
script.onreadystatechange = callback;
script.onload = callback;
// Fire the loading
head.appendChild(script);
}
var XJS;
var oldResponse;
function processText(response)
{
var responseCleaned;
if (LINEBREAK_BEHAVIOR == "Ignore Line Breaks")
{
responseCleaned = response.replace(/(\r\n|\n|\r)/gm,"");
}
else if (LINEBREAK_BEHAVIOR == "Replace")
{
responseCleaned = response.replace(/(\r\n|\n|\r)/gm, REPLACE_WITH);
}
else
{
responseCleaned = response.replace(/(\r\n|\n|\r)/gm,"<br>");
}
if (DELIMETER_1 != "" && DELIMETER_2 != "")
{
var responseCleanedLength = responseCleaned.length;
var indexOfDelim1 = responseCleaned.indexOf(DELIMETER_1) > -1 ? (responseCleaned.indexOf(DELIMETER_1) + DELIMETER_1.length) : 0;
var substringResponseCleaned = responseCleaned.substring(indexOfDelim1);
var substringResponseCleanedLength = substringResponseCleaned.length;
var initialIndexOfDelim2 = substringResponseCleaned.indexOf(DELIMETER_2) > -1 ? substringResponseCleaned.indexOf(DELIMETER_2) : responseCleanedLength;
var indexOfDelim2 = responseCleanedLength;
if (initialIndexOfDelim2 != responseCleanedLength)
{
indexOfDelim2 = initialIndexOfDelim2 + (responseCleanedLength - substringResponseCleanedLength);
}
if (indexOfDelim2 <= indexOfDelim1)
indexOfDelim2 = responseCleaned.length - 1;
responseCleaned = responseCleaned.substring(indexOfDelim1, indexOfDelim2);
}
if(oldResponse!=responseCleaned)
{
SetText(responseCleaned, "Local File: " + FILEPATH);
}
oldResponse=responseCleaned;
if (UPDATEINTERVAL > 0)
{
smlTitleTimeouts = setTimeout(function() { GetTextFromRemote(); }, UPDATEINTERVAL*1000);
}
}
function GetTextFromRemote(){
var response;
XJS.IO.getFileContent(FILEPATH).then(function(base64Content) {
try {
response = decodeURIComponent(escape(window.atob(base64Content)));
processText(response);
} catch(err) {
return new Promise(function(resolve, reject) {
reject("ERROR: " + err.stack);
});
}
}).catch(function(err) {
XJS.IO.getFileContent(FILEPATH).then(function(base64Content) {
response = window.atob(base64Content);
processText(response);
});
});
}
if (smlTitleTimeouts && smlTitleTimeouts != null)
{clearTimeout(smlTitleTimeouts);}
SetText("", "Local File: " + FILEPATH);
var loadScriptCallback = function() {
XJS = require('xjs');
XJS.ready()
.then(function() {
GetTextFromRemote()
})
};
loadScript(XJS_URL, loadScriptCallback);
&#13;
如您所见,它从XML文件加载代码,并从用户定义的两个分隔符之间获取所需的值。它还每秒自动更新文本以检查更改。另一个不太有用的功能是,如果我没有定义分隔符,它会加载整个文件,这对我来说很烦人,因为我没有使用某些值(例如当某人没有使用时) Twitter或赞助商标签)。我想要做的是获取在JSON文件中找到的数组而不是XML文件(因此DELIMITER_1可以是一个值,DELIMITER_2可以是另一个值)。我花了一些时间学习非常基本的Javascript,并设法得出这个:
var streamcontrol = {
"pName1": "TwilightStorm",
"pName2": "MannyFresh",
"pScore1": "0",
"pScore2": "0",
"pSponsor1": "Kern County eSports",
"pSponsor2": "Knova Gaming",
"pSponsorTag1": "KCeS",
"pSponsorTag2": "KVA",
"scoreboardHeader": "Casuals",
"timestamp": "1496028540"
};
var x = " "
SetText("[color=#ffd200]" + "[b]" + streamcontrol.pSponsorTag1 + "[/b]" + "[/color]" + x + streamcontrol.pName1)
&#13;
这给了我这个:
所以我的问题是:我如何让Xsplit加载streamcontrol.json,获取用户定义的值,然后以我在SetText命令中的格式输出它,同时仍然保持自动更新功能?我还想在每次更新值时添加一个fadeOut和fadeIn命令。
提前致谢!我希望从中学到很多东西。