User:Razzi/common.js

From Wikitech

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.
console.log('Welcome to Razzi user js!')

function toggleEdit() {
	const saveButton = document.getElementById('wpSave')
	if (saveButton) {
 		saveButton.click()
		return
 	}

	const visualPublishButton = document.querySelector("[data-old-accesskey='s']")
	if (visualPublishButton) {
		console.log('do keyboard shortcut for visual publish')

		// The button itself does not respond to click events, for whatever reason
		document.body.dispatchEvent(new KeyboardEvent('keydown', {key: 's', altKey: true, ctrlKey: true, bubbles: true, which: 83}))
		return
	}
	const visualEditButton = document.querySelector('#ca-ve-edit a')
	if (visualEditButton) {
		visualEditButton.click()
		return
	}

 	document.querySelector('li#ca-edit a').click()
}

document.onkeydown = function(e) {
	const mRet = e.metaKey && e.key === 'Enter'
	if (mRet) {
		toggleEdit()
	}
	if (e.metaKey && e.key === 's') {
		console.log('M-s')
		e.preventDefault()
		toggleEdit()
	}
}