Cannot apply settings from the sidebar gadget.

Well, I'm not an expert at Sidebar Gadgets, but I have this project that I will have to do for school. Tried to solve it on my own, but I'm really stuck.

I will need to use the Cirip API (Cirip.ro is a kind of Twitter), and to use the API I have to enter the username. I do this using the settings, but when I close the settings, the effect does not work.

Here is the code, but it's pretty dirty. I am not a web programmer, so javascript / html is new to me.

http://www.box.net/shared/7yogevhzrr


EDIT: Okay, so I narrowed it down a bit and optimized my code here, but it still doesn't work. I made it as simple as possible.

In the main html, I have this:

<script language="javascript">
    document.write(getUserName());
</script>

In the gadget.js file:

var userName;

document.onreadystatechange = function DoInit() {

    document.body.style.width = 251;
    document.body.style.height= 351;
    document.body.style.margin=2;
    userName="danieliuhasz";
    System.Gadget.settingsUI = "settings.html";
    System.Gadget.onSettingsClosing = settingsClosed;

}

function setUserName(userNameSet){

    userName = userNameSet;
}

function getUserName(){

    return userName;
}

function settingsClosed()
{

    var username = System.Gadget.Settings.read("username");
    setUserName(username);
}

In the settings.html file:

<body>

Username:<br />
<input name="userBox" type="text" maxlength="50" />
</body>

In settings.js:

document.onreadystatechange = function DoInit()
{    

    if(document.readyState=="complete")
    {
        var user = System.Gadget.Settings.read("userName");

        if(user != "")
        {
            userBox.value = user;
        }       
    }        
}


System.Gadget.onSettingsClosing = function(event)
{

    if (event.closeAction == event.Action.commit)
    {
        var username = userBox.value;
        if(username != "")
        {
              System.Gadget.Settings.write("username", username);
              System.Gadget.document.parentWindow.settingsClosed();

        }
    }
}

. "undefined".

+3
1

Rack- , , . , (. ).

System.Gadget.onSettingsClosing JavaScript-. , , , System.Gadget.Settings.write, . , / html :

<label for="username">Username: </label><input id="username" type="text"><br/>
<label for="password">Password: </label><input id="password" type="password">

JavaScript :

System.Gadget.onSettingsClosing = function (event) {
    // First, we need to make sure the user clicked the OK button to save
    if (event.closeAction == event.Action.commit) {
        // If they did, we need to store the settings
        var username = document.getElementById("username").value,
            password = document.getElementById("password").value;

        System.Gadget.Settings.write("username", username);
        System.Gadget.Settings.write("password", password);

        /* Finally, we can call a function defined in the main gadget window's
           JavaScript to let it know that the settings have changed */
        System.Gadget.document.parentWindow.settingsHaveChanged();
    }
}

JavaScript settingsHaveChanged :

function settingsHaveChanged () {
    var username = System.Gadget.Settings.read("username"),
        password = System.Gadget.Settings.read("password");

    sendAPIRequest(username, password);
}
+2

Source: https://habr.com/ru/post/1784044/


All Articles