User:Lockal/EditSum.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
(((mw) => {
    "use strict";

    if (!mw || !mw.config) {
        console.error("Unable to load EditSum: mw.config is not defined");
        return;
    }

    if (
        mw.config.get("wgAction") !== "view" ||
        !mw.config.get("wgIsProbablyEditable") ||
        !mw.config.get("wbIsEditView")
    ) {
        return;
    }

    const attachToBody = mw.config.get("wgNamespaceNumber") == 146; // lexeme
    const heading = document.querySelector(attachToBody ? "#mw-content-text" : ".mw-indicators");
    const attachClass = attachToBody ? "editsum-switch-inbody" : "editsum-switch-inheading";

    if (!heading) {
        console.log("No element to insert switch to");
        return;
    }

    const interceptableActions = [
    	"wbcreateclaim", "wbeditentity", 
    	"wblmergelexemes", "wbmergeitems", "wbremoveclaims", 
    	"wbremovequalifiers", "wbremovereferences", "wbsetaliases", 
    	"wbsetclaim", "wbsetclaimvalue", "wbsetdescription", "wbsetlabel", 
    	"wbsetqualifier", "wbsetreference", "wbsetsitelink"
    ];

    const styles = `
.editsum-switch-inheading {
    position: relative;
    margin-left: 5px;
}

.editsum-switch-inbody {
    position: absolute;
    right: 0;
}

.editsum-title {
    text-align: center;
    font-variant: small-caps;
    display: block;
    line-height: 14px;
}

/* switch styles */
.editsum-switch {
    cursor: pointer;
    user-select: none;
    display: inline-block;
    width: 60px;
    height: 34px;
}

.editsum-input {
    opacity: 0;
    width: 0;
    height: 0;
}

/* slider styles */
.editsum-slider {
    position: absolute;
    top: 16px;
    left: 12px;
    right: 12px;
    bottom: 0;
    background-color: #ccc;
    -webkit-transition: .2s;
    transition: .2s;
    border-radius: 34px;
}

.editsum-slider:before {
    position: absolute;
    content: "";
    height: 10px;
    width: 10px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    -webkit-transition: .2s;
    transition: .2s;
    border-radius: 50%;
}

.editsum-input:checked + .editsum-slider {
    background-color: #2196F3;
}

.editsum-input:focus + .editsum-slider {
    box-shadow: 0 0 1px #2196F3;
}

.editsum-input:checked + .editsum-slider:before {
    -webkit-transform: translateX(18px);
    -ms-transform: translateX(18px);
    transform: translateX(18px);
}
`;
    let styleSheet = document.createElement("style");
    styleSheet.type = "text/css";
    styleSheet.innerText = styles;
    document.head.appendChild(styleSheet);

    let editsum_switch = document.createElement("label");
    editsum_switch.classList.add("editsum-switch");
    editsum_switch.classList.add(attachClass);

    let editsum_title = document.createElement("span");
    editsum_title.appendChild(document.createTextNode("EditSum"));
    editsum_title.classList.add("editsum-title");

    let editsum_input = document.createElement("input");
    editsum_input.type = "checkbox";
    editsum_input.classList.add("editsum-input");
    editsum_input.onchange = function() {
        setupInterceptor(editsum_input.checked);
    };

    let editsum_slider = document.createElement("span");
    editsum_slider.classList.add("editsum-slider");

    editsum_switch.appendChild(editsum_title);
    editsum_switch.appendChild(editsum_input);
    editsum_switch.appendChild(editsum_slider);

    heading.insertBefore(editsum_switch, heading.firstChild);
    
	const promptUser = (message, placeholder, value) => {
		mw.loader.using('oojs-ui-core').then(() => {
			return OO.ui.prompt(message, {
				textInput: { placeholder: placeholder, value: value }
			});
		});
	};

	const origPost = mw.Api.prototype.post;
	const patchedPost = function(parameters, ajaxOptions) {
		if (parameters && interceptableActions.includes(parameters.action)) {
        	const msg = "Enter the summary for the " + parameters.action +  " action. " +
			        	"Will be prepended by an automatically generated comment. " +
			        	"You can also notify specific users by entering [[User:Username]].";
			return mw.loader.using('oojs-ui-core').then(() => {
				return OO.ui.prompt(msg, {
					size: "medium",
					textInput: {
						placeholder: "Summary",
						value: parameters.summary,
						maxLength: 260,
					}
				}).then((summary) => {
					if (summary !== null) {
						arguments[0].summary = summary;
						console.log("Summary was changed", summary);
					}
					return origPost.apply(this, arguments);
				});
			});
		} else {
	    	return origPost.apply(this, arguments);
		}
	};

    const setupInterceptor = function(enabled) {
        if (enabled) {
            console.log("Summary interceptor was enabled");
            mw.Api.prototype.post = patchedPost;
        } else {
            console.log("Summary interceptor was disabled");
            mw.Api.prototype.post = origPost;
        }
    };

}))(window.mw);