Last active
April 1, 2016 08:47
-
-
Save bunyk/6ae97e5c3de490cfb4a1 to your computer and use it in GitHub Desktop.
This file contains 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
console.log('Почато завантаження букмарклета'); | |
(function(){ | |
var get_meta_property = function(name) { | |
var element = document.querySelector('meta[property="' + name + '"]'); | |
return element && element.getAttribute("content") || ''; | |
}; | |
var get_article_time = function() { | |
var text = get_meta_property('article:modified_time'); | |
if(text) { | |
return format_date(new Date(text)); | |
} else { | |
return ''; | |
}; | |
}; | |
var get_author = function() { | |
return document.querySelector('a[rel=author] [itemprop=name]').innerHTML; | |
}; | |
/** Now we can create new site handlers that will reuse functionality of the default site handler. */ | |
function SiteHandler(params) { | |
this.get_title = params.get_title || function () { return document.title; }; | |
this.get_website = params.get_website || function () { return get_meta_property('og:site_name'); }; | |
this.get_publisher = params.get_publisher || function () { return get_meta_property('article:publisher'); }; | |
this.get_date = params.get_date || function () { return get_article_time(); }; | |
this.get_author = params.get_author || function () { return get_author(); }; | |
} | |
var siteHandlers = { | |
'na.mil.gov.ua' : new SiteHandler ({ | |
get_website : function () { return 'Народна Армія'; }, | |
get_publisher : function () { return 'Міністерство Оборони України'; } | |
}) | |
}; | |
var get_template_text = function () { | |
var domain = window.location.hostname; | |
var handler = ( siteHandlers[domain] || new SiteHandler({}) ); | |
return '<ref name="">{{cite web\n' + | |
'|url = ' + window.location.href + '\n' + | |
'|title = ' + handler.get_title() + '\n' + | |
'|author = ' + handler.get_author() + '\n' + | |
'|date = ' + handler.get_date() + '\n' + | |
'|website = ' + handler.get_website() + '\n' + | |
'|publisher = ' + handler.get_publisher() + '\n' + | |
'|accessdate = ' + format_date(new Date()) + '\n' + | |
'}}</ref>' | |
.replace('|', '|') // escape template delimiters | |
.replace('\n|', '\n|') // but undo delimiters at the beginning of line | |
.replace('&', '&') // escape html | |
.replace('<', '<'); | |
}; | |
var init = function () { | |
console.log('Очікування завантаження сторінки'); | |
jQuery(function($){ | |
console.log('Створюємо вікно'); | |
$(css_style).appendTo("head"); | |
var panel = $('<div class="cite_web_panel"></div>'); | |
var header = $('<div class="header">Ctrl+C щоб скопіювати</div>'); | |
var close = $('<a href="#">Закрити</span>').click(function() { | |
panel.remove(); | |
}); | |
var content = $('<textarea>' + get_template_text() + '</textarea>'); | |
header.append(close); | |
panel.append(header); | |
panel.append(content); | |
$('body').append(panel); | |
SelectText(content[0]); | |
wikiBookmarkletDone(); // remove style with wait cursor | |
console.log('Готово!') | |
}); | |
}; | |
var SelectText = function (element) { | |
// http://stackoverflow.com/a/987376/816449 | |
var doc = document, range, selection; | |
if (doc.body.createTextRange) { | |
range = document.body.createTextRange(); | |
range.moveToElementText(element); | |
range.select(); | |
} else if (window.getSelection) { | |
selection = window.getSelection(); | |
range = document.createRange(); | |
range.selectNodeContents(element); | |
selection.removeAllRanges(); | |
selection.addRange(range); | |
} | |
}; | |
var monthNames = [ | |
"січня", "лютого", | |
"березня", "квітня", "травня", | |
"червня", "липня", "серпня", | |
"вересня", "жовтня", "листопада", | |
"грудня" | |
]; | |
var format_date = function(date) { | |
var day = date.getDate(); | |
var monthIndex = date.getMonth(); | |
var year = date.getFullYear(); | |
return day + ' ' + monthNames[monthIndex] + ' ' + year; | |
}; | |
console.log('Очікуємо завантаження jQuery'); | |
if (window.jQuery === undefined) { | |
var script = document.createElement("script"); | |
script.src = "https://code.jquery.com/jquery-1.12.0.min.js"; | |
script.onload = init; | |
document.body.appendChild(script); | |
} else { | |
init(); | |
}; | |
var css_style = '' + | |
'<style type="text/css">' + | |
'.cite_web_panel {' + | |
' position: fixed;' + | |
' left: 20%;' + | |
' top: 10%;' + | |
' width: 60%;' + | |
' height: 80%;' + | |
' overflow: auto;' + | |
' border: 1px solid black;' + | |
' background: white;' + | |
' z-index: 9999;' + | |
'}' + | |
'.cite_web_panel .header {' + | |
' width: 100%;' + | |
' background: lightblue;' + | |
' border: 1px solid black;' + | |
' box-sizing: border-box;' + | |
' padding: 2px;' + | |
' font-family: sans-serif;' + | |
' font-weight: bold;' + | |
'}' + | |
'.cite_web_panel .header a {' + | |
' float: right;' + | |
'}' + | |
'.cite_web_panel textarea {' + | |
' width: 100%;' + | |
' margin: 0px;' + | |
' padding: 15px;' + | |
' box-sizing: border-box;' + | |
' font-family: monospace;' + | |
'}' + | |
'</style>'; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment