Jump to content

User:Anpang/Wikipe-tan Chatbox.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Anpang (talk | contribs) at 11:21, 30 October 2023. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// get css
importStylesheet("User:Anpang01/Wikipe-tan_Chatbox.css");

// add box
const wptcGreeting = "👋 Hello! What would you like me to help with?";

$("body").append(`
    <div class="wptc-box" id="wptc-box">
        <div>
            <img src="/media/wikipedia/commons/a/ac/Wikipe-tan_head.png" class="wptc-image" />
            <span class="wptc-name">Wikipe-tan</span>
            <a href="#" class="wptc-close" id="wptc-close">&times;</a>
        </div>
        <div class="wptc-inner-box">
            <div>
                <div id="wptc-bubbles">
            		<div class="wptc-bubble-left">
                    	<span>${wptcGreeting}</span>
                	</div>
                </div>
                <input type="text" class="wptc-input" id="wptc-input" />
            </div>
        </div>
        <span class="wptc-bottom-links">
            &ensp;
            <a href="https://en.wikipedia.org/wiki/User:Anpang01/Wikipe-tan_Chatbox">JS script</a>
            &ensp;
            <a href="https://en.wikipedia.org/wiki/Wikipedia:Wikipe-tan">Based on Wikipe-tan</a>
            &ensp;
        </span>
    </div>
`);

// close box handler
$("#wptc-close").on("click", function() {
	$("#wptc-box").hide();
});

// get response
function wptcGetResponse(input) {
    return "Sorry, I can't communicate with you at the moment.";
}

// submit handler
$("#wptc-input").keypress(function(key) {
    if(key.which === 13) { // is enter key
        // get and clear input
        let input = $("#wptc-input").val();
        $("#wptc-input").val("");
        
        // do stuff
        $("#wptc-bubbles").append(`
            <div class="wptc-bubble-right">
                <span>${input}</span>
            </div>
            <div class="wptc-bubble-left">
                <span>${wptcGetResponse(input)}</span>
            </div>
        `);
    }
});