Skip to content

Instantly share code, notes, and snippets.

@imekachi
Last active March 2, 2021 07:46
Show Gist options
  • Save imekachi/521120e2357088995d0d11f0ee752940 to your computer and use it in GitHub Desktop.
Save imekachi/521120e2357088995d0d11f0ee752940 to your computer and use it in GitHub Desktop.
Custom JS, add a button to copy username in Line 1-on-1 chat
function getName() {
return getNameElement().textContent
}
function getNameElement() {
return document.querySelector('#_chat_header_area h1')
}
function copyText(text) {
const inputId = 'copy-input'
let input = document.getElementById(inputId)
console.log(`> input: `, input)
if (!input) {
const inputEl = document.createElement('input')
inputEl.id = inputId
inputEl.type = 'text'
document.body.appendChild(inputEl)
input = inputEl
}
input.value = text
input.select()
document.execCommand('copy')
input.remove()
alert('Copied')
}
function handleClickCopyButton() {
const name = getName()
console.log(`name: ${name}`)
copyText(name)
}
function addCopyButton() {
const buttonId = 'custom-copy-button'
try {
document.getElementById(buttonId).remove()
} catch(err) {
//
}
const button = document.createElement('button')
button.textContent = `Copy username`
button.id = buttonId
button.style.cssText = `
position: fixed;
top: 54px;
right: 15px;
background-color: #f37A01;
padding: 10px;
color: white;
border-radius: 3px;
z-index: 100;
`
button.addEventListener('click', handleClickCopyButton)
document.body.appendChild(button)
return button
}
addCopyButton()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment