2025-11-28 00:35:46 +09:00

122 lines
4.0 KiB
JavaScript

/*************************************************************************
*
* File: Debug.js
*
* Description:
* Script for the "Debug" Desktop Gadget sample.
* This script attempts to write to a text file specified by the user
* in the Settings dialog. An error is thrown and the error details
* written to the System.Debug output string if the filename does
* not contain a period.
*
* NOTE:
* The Microsoft utility 'DebugView for Windows' can be used to
* view the error message generated by the System.Debug.outputString
* in this sample. DebugView is an application that lets you monitor
* debug output on your local system, or any computer on the network
* that you can reach via TCP/IP. It is capable of displaying both
* kernel-mode and Win32 debug output, so you don't need a debugger
* to catch the debug output this gadget generates.
*
* This file is part of the Microsoft Windows SDK Code Samples.
*
* Copyright (C) Microsoft Corporation. All rights reserved.
*
* This source code is intended only as a supplement to Microsoft
* Development Tools and/or on-line documentation. See these other
* materials for detailed information regarding Microsoft code samples.
*
* THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*
************************************************************************/
// Member variables.
var sFilename = "";
var sFileContent = "";
var sFilePath = System.Gadget.path + "\\";
// Open a file for writing (1 - reading, 8 - appending).
var FOR_WRITING = 2;
// Allow the gadget to create a new file, if necessary.
var CREATE_NEW = true;
// Initialize the gadget.
function Init()
{
// Enable the gadget settings functionality.
System.Gadget.settingsUI = "settings.html";
System.Gadget.onSettingsClosed = SettingsClosed;
// Retrieve an existing file name and content, if any.
sFilename =
System.Gadget.Settings.readString("settingsFilename");
sFileContent = System.Gadget.Settings.readString("settingsContent");
// Initialize the gadget content.
SetGadgetContent();
}
// Set the text of the gadget based on the user input;
// execute this function at startup and after settings changes.
// Note: User input may require verification.
function SetGadgetContent()
{
if (String(sFilename) != "")
{
// Display output string and save button.
divInstructions.style.display = "none";
divFileInfo.style.display = "block";
spanFilename.innerText = sFilename;
divFileContent.innerText = sFileContent;
// innerHTML is OK here, as we're not inserting untrusted data
spanFileControls.innerHTML =
'<input id="btnSave" type="button" onclick="SaveFile()" ' +
'title="Save file to disk." value="Save" />'
}
else
{
divInstructions.style.display = "block";
divFileInfo.style.display = "none";
}
}
// Attempt to save the file and throw an error if the filename does
// not include a file extension.
function SaveFile()
{
var fso = new ActiveXObject( "Scripting.FileSystemObject" );
// Throw an arbitrary error to demonstrate the outputString method.
try
{
if (sFilename.indexOf(".") == -1)
{
throw "'Filename' does not specify a file extension.";
}
var file =
fso.OpenTextFile(sFilePath + sFilename,
FOR_WRITING, CREATE_NEW);
file.Write(sFileContent);
file.Close();
fso = file = null;
}
catch(e)
{
System.Debug.outputString(e);
}
}
// Handle the Settings dialog closed event.
// event = Event argument.
function SettingsClosed(event)
{
if (event.closeAction == event.Action.commit)
{
sFilename =
System.Gadget.Settings.readString("settingsFilename");
sFileContent =
System.Gadget.Settings.readString("settingsContent");
SetGadgetContent();
}
}