// ==UserScript== // @name Confluence Auto Numbered Headings // @namespace https://gist.github.com/elahd/28f64feddd9ece56f4f0566d195d0cbd // @version 0.7 // @description Adds numbered headings button to the page editor in Atlassian Confluence. Based on work by Markus Jenu at https://community.atlassian.com/t5/Confluence-questions/Is-it-possible-to-add-numbering-to-headings-in-Confluence/qaq-p/315517#M87046. // @author Elahd Bar-Shai // @match https://*.atlassian.net/wiki/spaces/* // @match https://*.atlassian.net/wiki/spaces/* // @require https://greasyfork.org/scripts/383527-wait-for-key-elements/code/Wait_for_key_elements.js?version=701631 // @grant none // @downloadURL https://update.greasyfork.icu/scripts/381252/Confluence%20Auto%20Numbered%20Headings.user.js // @updateURL https://update.greasyfork.icu/scripts/381252/Confluence%20Auto%20Numbered%20Headings.meta.js // ==/UserScript== (function() { 'use strict'; var jQuery = window.jQuery; var old_conf; function addIndex() { var indices = []; if (old_conf) { jQuery("#wysiwygTextarea_ifr").contents().find("h1,h2,h3,h4,h5,h6").each(function(i,e) { var hIndex = parseInt(this.nodeName.substring(1)) - 1; if (indices.length - 1 > hIndex) { indices= indices.slice(0, hIndex + 1 ); } if (indices[hIndex] == undefined) { indices[hIndex] = 0; } indices[hIndex]++; jQuery(this).html(indices.join(".")+". " + removeNo(jQuery(this).html())); }); } else { jQuery(".ak-editor-content-area .ProseMirror").find("h1,h2,h3,h4,h5,h6").each(function(i,e) { var hIndex = parseInt(this.nodeName.substring(1)) - 1; if (indices.length - 1 > hIndex) { indices= indices.slice(0, hIndex + 1 ); } if (indices[hIndex] == undefined) { indices[hIndex] = 0; } indices[hIndex]++; jQuery(this).html(indices.join(".")+". " + removeNo(jQuery(this).html())); }); } } function removeNo(str) { let newstr = str.trim(); newstr = newstr.replace(/[\u00A0\u1680​\u180e\u2000-\u2009\u200a​\u200b​\u202f\u205f​\u3000]/g,' '); if(IsNumeric(newstr.substring(0,newstr.indexOf(' ')))){ return newstr.substring(newstr.indexOf(' ')+1).trim(); } return newstr; } function IsNumeric(num) { num = num.split('.').join(""); return (num >=0 || num < 0); } function createButton () { old_conf = jQuery('.ProseMirror').length == 0 ? true : false; if (old_conf) { window.AJS.toInit(() => { jQuery('#rte-toolbar > div.aui-toolbar2-primary.toolbar-primary').append('
') jQuery("#addIndex").click(function(e) { e.preventDefault(); addIndex(); }); }); } else { jQuery('.sc-kafWEX.eAQEwn > span').last().after('