Skip to content

Instantly share code, notes, and snippets.

@nachocab
Last active January 10, 2018 13:27
Show Gist options
  • Save nachocab/6c985344d909d5a8a1fad60b4e1b5344 to your computer and use it in GitHub Desktop.
Save nachocab/6c985344d909d5a8a1fad60b4e1b5344 to your computer and use it in GitHub Desktop.
Treccani
// Custom JavaScript for Websites Chrome Extension Script for
// the italian dictionary www.treccani.it
// Author: Nacho - NachoTime Spanish
// URL: itsnachotime.com
// License: MIT
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = `
.accezione + .sottoaccezione hr {
display: none;
}
`;
document.head.appendChild(style);
document.head.style = style;
var accezioneTags = document.querySelectorAll('.accezione');
var sottoaccezioneTags = document.querySelectorAll('.sottoaccezione');
var emTags = document.querySelectorAll('.testo_corsivo');
var container = document.querySelector('.module-article-full_content');
if (container) container.style = 'margin-top: 0';
var mainContent = document.querySelector('.module-article-full_content p');
if (mainContent) mainContent.style = 'line-height: 1.4; font-family: Helvetica Neue';
var share = document.querySelector('.module-share');
if (share) share.style = 'display: none';
var pdf = document.querySelector('.module-pdf-creator');
if (pdf) pdf.style = 'display: none';
var briciole = document.querySelector('.module-briciole_di_pane');
if (briciole) briciole.style = 'display: none';
var studenti = document.querySelector('.top-studenti_it-wrapper');
if (studenti) studenti.style = 'display: none';
var moduleSearch = document.querySelector('.module-search');
if (moduleSearch) moduleSearch.style = 'padding: 10px 0';
var treccani = document.querySelector('.module.module-header');
if (treccani) treccani.style = 'display: none';
Array.from(accezioneTags).forEach(tag => {
var breakLine = document.createElement('hr');
breakLine.style = 'line-height: 2em; border-top: 1px solid black';
tag.insertBefore(breakLine, tag.firstChild);
});
Array.from(sottoaccezioneTags).forEach(tag => {
var breakLine = document.createElement('hr');
breakLine.style = 'line-height: 1.6em; border-top: 1px solid #ddd';
tag.insertBefore(breakLine, tag.firstChild);
});
Array.from(emTags).forEach(tag => {
var strong = document.createElement('strong');
strong.textContent = tag.textContent;
tag.outerHTML = strong.outerHTML;
});
const word = location.href.match(/.+\/([^/]+)\/?$/)[1];
function newLink(opts) {
let link = document.createElement('a');
link.href = opts.href;
link.innerHTML = opts.content;
link.target = opts.newTab ? '_blank' : '';
link.style = opts.style;
return link;
}
const glosbeLink = newLink({
href: `https://glosbe.com/it/es/${word}`,
content: ' (Spagnolo)',
style: 'font-size: 3rem; margin-left: 20px; color: #6e6f6e',
newTab: true
});
const title = document.querySelector('h1.title-search');
title.appendChild(glosbeLink);
// for some reason, there are duplicate #search_what elements, so we have
// to remove one for focus() to work
var input = document.querySelector('#search_what');
input.remove();
input = document.querySelector('#search_what');
input.focus();
document.addEventListener('visibilitychange', e => {
if (!document.hidden) {
input.focus();
}
});
document.addEventListener('keydown', e => {
if (e.keyCode == 27) {
input.focus();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment