MediaWiki:Common.js

From H&D Publishing Wiki
Revision as of 13:05, 5 April 2023 by Karl (talk | contribs)

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.
// Any JavaScript here will be loaded for all 
// users on every page load. 

console.log('hello from common.js')

// rename 'Discussion' tab or context menu button 
// to 'CSS' in the 'Publishing' namespace.

const 
	url    = window.location.href,
	NS     = 'Publishing',         // content namespace
	cssNS  = NS + 'CSS',           // css namespace
	jsNS   = NS + 'JS'             // js namespace


const pageViews = document.querySelector('#p-views ul')

if (url.includes(NS + ':')) {
	console.log('this page is in namespace', NS)
	
	const contentButton = document.createElement('li')
	contentButton.classList.add('collapsible', 'mw-list-item')
	contentButton.id = 'ca-content'
	contentButton.innerHTML = '<a href="' + url + '">Content!</a>'
	pageViews.appendChild(contentButton)
	
	const talkAnchor = document.querySelector('#ca-talk a')
	const talkLink = talkAnchor.href
	const cssButton  = document.createElement('li')
	cssButton.classList.add('collapsible', 'mw-list-item')
	cssButton.id = 'ca-css'
	cssButton.innerHTML = '<a href="' + talkLink + '">CSS!</a>'
	pageViews.appendChild(cssButton)

	const jsLink = talkAnchor.href.replace( cssNS, jsNS )
	const jsButton  = document.createElement('li')
	jsButton.classList.add('collapsible', 'mw-list-item')
	jsButton.id = 'ca-js'
	jsButton.innerHTML = '<a href="' + jsLink + '">JS!</a>'
	pageViews.appendChild(jsButton)


} else if (url.includes(cssNS + ':')) {
	console.log('this page is in namespace', cssNS)
	
	const contentAnchor = document.querySelector('#ca-nstab-publishing a')
	const contentLink = contentAnchor.href
	const contentButton = document.createElement('li')
	contentButton.classList.add('collapsible', 'mw-list-item')
	contentButton.id = 'ca-content'
	contentButton.innerHTML = '<a href="' + contentLink + '">Content!</a>'
	pageViews.appendChild(contentButton)
	
	const cssButton  = document.createElement('li')
	cssButton.classList.add('collapsible', 'mw-list-item')
	cssButton.id = 'ca-css'
	cssButton.innerHTML = '<a href="' + url + '">CSS!</a>'
	pageViews.appendChild(cssButton)
	
	const jsLink = contentAnchor.href.replace( NS, jsNS )
	const jsButton  = document.createElement('li')
	jsButton.classList.add('collapsible', 'mw-list-item')
	jsButton.id = 'ca-js'
	jsButton.innerHTML = '<a href="' + jsLink + '">JS!</a>'
	pageViews.appendChild(jsButton)
	
	const addTopicButton = document.querySelector('#ca-addsection')
	pageViews.removeChild(addTopicButton)	
		
		
} else if (url.includes(jsNS + ':')) {
	console.log('this page is in namespace', jsNS)

	const contentLink = url.replace( jsNS, NS )
	const contentButton = document.createElement('li')
	contentButton.classList.add('collapsible', 'mw-list-item')
	contentButton.id = 'ca-content'
	contentButton.innerHTML = '<a href="' + contentLink + '">Content!</a>'
	pageViews.appendChild(contentButton)
	
	const cssLink = url.replace( jsNS, cssNS )
	const cssButton  = document.createElement('li')
	cssButton.classList.add('collapsible', 'mw-list-item')
	cssButton.id = 'ca-css'
	cssButton.innerHTML = '<a href="' + cssLink + '">CSS!</a>'
	pageViews.appendChild(cssButton)
	
	const jsLink = url
	const jsButton  = document.createElement('li')
	jsButton.classList.add('collapsible', 'mw-list-item')
	jsButton.id = 'ca-js'
	jsButton.innerHTML = '<a href="' + jsLink + '">JS!</a>'
	pageViews.appendChild(jsButton)
			
}