Last active
July 7, 2023 10:58
-
-
Save goeko/7a05cc4909b6e80d5f1fd97558e10da6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:(function() { | |
var tooltipElements = document.querySelectorAll('[data-tooltip-template="web.FieldTooltip"]'); | |
if (!tooltipElements || tooltipElements.length === 0) { | |
alertTooltips('Die Tooltipelemente wurden nicht gefunden.'); | |
return; | |
} | |
var alertsContainer = document.getElementById('alertTooltipsContainer'); | |
if (!alertsContainer || alertsContainer === null) { | |
alertsContainer = document.createElement('div'); | |
alertsContainer.id = 'alertTooltipsContainer'; | |
buttonsContainer = document.createElement('div'); | |
buttonsContainer.id = 'buttonsContainer'; | |
alertsContainer.appendChild(buttonsContainer); | |
document.body.appendChild(alertsContainer); | |
var style = document.createElement('style'); | |
style.innerHTML = ` | |
[data-tooltip-template="web.FieldTooltip"] { | |
font-size: 14px; | |
} | |
#alertTooltipsContainer { | |
position: fixed; | |
bottom: 10px; | |
right: 10px; | |
z-index: 9999; | |
display: flex; | |
flex-direction: column; | |
align-items: flex-end; | |
background-color: rgba(255,255,255,.6); | |
} | |
#alertTooltipsContainer #buttonsContainer { | |
display: flex; | |
flex-direction: row; | |
align-items: flex-end; | |
} | |
#alertTooltipsContainer button { | |
padding: 5px; | |
border-radius: 5px; | |
border: 1px solid #ddd; | |
background-color: smokewhite; | |
cursor: pointer; | |
margin-bottom: 10px; | |
margin-left: 8px; | |
shadow: 0 0 5px #ddd; | |
} | |
#alertTooltipsContainer button:hover { | |
background-color: #f0f0f0; | |
} | |
#alertTooltipsContainer button:active { | |
background-color: #e0e0e0; | |
} | |
.alertDiv { | |
border-top: 1px solid gray; | |
padding-top: 0.5em; | |
margin-top: 0.5em; | |
margin-bottom: 1em; | |
} | |
`; | |
document.head.appendChild(style); | |
var createClipboardButton = function(id, localStorageName, buttonText) { | |
var btnClipboard = document.createElement('button'); | |
btnClipboard.id = 'btn_'+localStorageName; | |
btnClipboard.innerText = buttonText; | |
btnClipboard.addEventListener('click', function() { | |
copyToClipboard(localStorageName); | |
}, false); | |
buttonsContainer.appendChild(btnClipboard); | |
}; | |
createClipboardButton(1, 'odoo_resModel', 'CP Model'); | |
createClipboardButton(2, 'odoo_values', 'CP Values'); | |
createClipboardButton(3, 'odoo_fieldName', 'CP Fields'); | |
var btnResetValues = document.createElement('button'); | |
btnResetValues.id = 'reset_btn'; | |
btnResetValues.innerText = 'Werte zurücksetzen'; | |
btnResetValues.addEventListener('click', function() { | |
resetValues(); | |
}, false); | |
buttonsContainer.appendChild(btnResetValues); | |
} | |
function handleTooltipHover(event) { | |
var tooltipElement = event.target; | |
var tooltipInfo = tooltipElement.getAttribute('data-tooltip-info'); | |
if (!tooltipInfo) { | |
alertTooltips('Die Tooltip-Informationen konnten nicht gefunden werden.'); | |
return; | |
} | |
var tooltipData; | |
try { | |
tooltipData = JSON.parse(tooltipInfo); | |
} catch (error) { | |
alertTooltips('Die Tooltip-Daten konnten nicht analysiert werden.'); | |
return; | |
} | |
var resModel = tooltipData.resModel; | |
var fieldName = tooltipData.field.name; | |
if (!resModel || !fieldName) { | |
var currentURL = window.location.href; | |
var url = new URL(currentURL); | |
var debugParam = url.searchParams.get('debug'); | |
if (debugParam && debugParam === '1') { | |
return; | |
} | |
url.searchParams.set('debug', '1'); | |
var newURL = url.toString(); | |
window.location.href = newURL; | |
} else { | |
var key = resModel + '.' + fieldName; | |
storeProtocol(key); | |
storeLocalStorageValue(key, 'odoo_values'); | |
storeLocalStorageValue(resModel, 'odoo_resModel'); | |
storeLocalStorageValue(fieldName, 'odoo_fieldName'); | |
copyToClipboard(key, true); | |
} | |
} | |
function alertTooltips(message, background, delay) { | |
var alertDiv = document.createElement('div'); | |
alertDiv.classList.add("alertDiv"); | |
alertDiv.innerText = message; | |
alertsContainer.appendChild(alertDiv); | |
setTimeout(function() { | |
alertDiv.style.opacity = '1'; | |
}, 100); | |
setTimeout(function() { | |
alertDiv.style.opacity = '0'; | |
setTimeout(function() { | |
if (alertDiv && alertDiv.parentNode) { | |
alertDiv.parentNode.removeChild(alertDiv); | |
} | |
}, 500); | |
}, delay || 2000); | |
} | |
function storeProtocol(key) { | |
var protocol = localStorage.getItem('odoo_protocol') || ''; | |
protocol += key + '\n'; | |
localStorage.setItem('odoo_protocol', protocol); | |
var count = localStorage.getItem('odoo_protocol_count') || 0; | |
count++; | |
localStorage.setItem('odoo_protocol_count', count); | |
if (count % 100 === 0) { | |
alertTooltips('Odoo Protocol hat jetzt ' + count + ' Einträge', '#ffa500', 10000); | |
} | |
if (count === 1024) { | |
localStorage.setItem('odoo_protocol', ''); | |
localStorage.setItem('odoo_protocol_count', '0'); | |
alertTooltips('Odoo Protocol hat jetzt ' + count + ' Einträge und wurde geleert', '#ff0000', 10000); | |
} | |
} | |
function storeLocalStorageValue(value, localStorageName) { | |
var values = localStorage.getItem(localStorageName) || ''; | |
if (!values.includes(value)) { | |
values += value + '\n'; | |
localStorage.setItem(localStorageName, values); | |
} | |
} | |
function copyToClipboard(localStorageName, isString) { | |
var value = (isString===true) ? localStorageName : localStorage.getItem(localStorageName); | |
if (value) { | |
navigator.clipboard.writeText(value) | |
.then(function() { | |
alertTooltips('Der Wert wurde erfolgreich in die Zwischenablage kopiert:\n' + value); | |
}) | |
.catch(function() { | |
alertTooltips('Beim Kopieren des Werts ist ein Fehler aufgetreten.'); | |
}); | |
} else { | |
alertTooltips('Es gibt keinen Wert zum Kopieren.'); | |
} | |
} | |
function resetValues() { | |
localStorage.removeItem('odoo_values'); | |
localStorage.removeItem('odoo_resModel'); | |
localStorage.removeItem('odoo_fieldName'); | |
alertTooltips('Die Werte wurden zurückgesetzt.', '#ff0000', 5000); | |
} | |
tooltipElements.forEach(function(tooltipElement) { | |
if (!tooltipElement.hasEventListener) { | |
tooltipElement.addEventListener('click', handleTooltipHover); | |
tooltipElement.hasEventListener = true; | |
} | |
}); | |
var currentURL = window.location.href; | |
var url = new URL(currentURL); | |
var debugParam = url.searchParams.get('debug'); | |
if (debugParam && debugParam === '1') { | |
alertTooltips('Das Bookmarklet wurde bereits initialisiert.'); | |
} else { | |
alertTooltips('Das Bookmarklet wurde initialisiert.'); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment