PageSourceSearch

https://krknews.pl/wp-content/themes/krknews/js/share.js?ver=1.2.0

js krknews.pl collected 2026-10-02 14:41:20 UTC 7,190 bytes, 162 lines download raw bytes

1/**
2 * File share.js
3 *
4 * Dropdown udostępniania artykułów w social media i SMS.
5 * Obsługuje wszystkie przyciski z atrybutem data-share-url.
6 */
7( function() {
8	'use strict';
9
10	var activeDropdown = null;
11
12	/**
13	 * Kopiuje tekst do schowka z fallbackiem dla HTTP.
14	 */
15	function copyToClipboard( text ) {
16		if ( navigator.clipboard && window.isSecureContext ) {
17			return navigator.clipboard.writeText( text );
18		}
19
20		var textarea = document.createElement( 'textarea' );
21		textarea.value = text;
22		textarea.style.position = 'fixed';
23		textarea.style.left = '-9999px';
24		document.body.appendChild( textarea );
25		textarea.select();
26
27		return new Promise( function( resolve, reject ) {
28			if ( document.execCommand( 'copy' ) ) {
29				resolve();
30			} else {
31				reject();
32			}
33			document.body.removeChild( textarea );
34		} );
35	}
36
37	// SVG ikony.
38	var icons = {
39		facebook: '<svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor"><path d="M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"/></svg>',
40		twitter: '<svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor"><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/></svg>',
41		whatsapp: '<svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z"/></svg>',
42		sms: '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>',
43		copy: '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>'
44	};
45
46	/**
47	 * Tworzy HTML dropdownu.
48	 */
49	function createDropdown( url, title ) {
50		var encoded = encodeURIComponent( url );
51		var encodedTitle = encodeURIComponent( title );
52
53		var div = document.createElement( 'div' );
54		div.className = 'share-dropdown';
55		div.innerHTML =
56			'<a class="share-dropdown__item" data-share-action="facebook" data-href="https://www.facebook.com/sharer/sharer.php?u=' + encoded + '">' +
57				'<span class="share-dropdown__icon">' + icons.facebook + '</span>' +
58				'<span class="share-dropdown__label">Facebook</span>' +
59			'</a>' +
60			'<a class="share-dropdown__item" data-share-action="twitter" data-href="https://twitter.com/intent/tweet?url=' + encoded + '&text=' + encodedTitle + '">' +
61				'<span class="share-dropdown__icon">' + icons.twitter + '</span>' +
62				'<span class="share-dropdown__label">X (Twitter)</span>' +
63			'</a>' +
64			'<a class="share-dropdown__item" data-share-action="whatsapp" data-href="https://wa.me/?text=' + encodedTitle + '%20' + encoded + '">' +
65				'<span class="share-dropdown__icon">' + icons.whatsapp + '</span>' +
66				'<span class="share-dropdown__label">WhatsApp</span>' +
67			'</a>' +
68			'<a class="share-dropdown__item" data-share-action="sms" href="sms:?body=' + encodedTitle + '%20' + encoded + '">' +
69				'<span class="share-dropdown__icon">' + icons.sms + '</span>' +
70				'<span class="share-dropdown__label">SMS</span>' +
71			'</a>' +
72			'<a class="share-dropdown__item" data-share-action="copy" data-url="' + url + '">' +
73				'<span class="share-dropdown__icon">' + icons.copy + '</span>' +
74				'<span class="share-dropdown__label">Kopiuj link</span>' +
75			'</a>';
76
77		return div;
78	}
79
80	/**
81	 * Zamyka aktywny dropdown.
82	 */
83	function closeDropdown() {
84		if ( activeDropdown ) {
85			activeDropdown.remove();
86			activeDropdown = null;
87		}
88	}
89
90	/**
91	 * Główny handler kliknięć.
92	 */
93	document.addEventListener( 'click', function( e ) {
94		// Klik na element dropdownu.
95		var dropdownItem = e.target.closest( '.share-dropdown__item' );
96		if ( dropdownItem ) {
97			e.preventDefault();
98			var action = dropdownItem.getAttribute( 'data-share-action' );
99
100			if ( action === 'copy' ) {
101				var copyUrl = dropdownItem.getAttribute( 'data-url' );
102				copyToClipboard( copyUrl ).then( function() {
103					var label = dropdownItem.querySelector( '.share-dropdown__label' );
104					label.textContent = 'Skopiowano!';
105					label.classList.add( 'share-dropdown__label--copied' );
106					setTimeout( function() {
107						closeDropdown();
108					}, 1000 );
109				} );
110			} else if ( action === 'sms' ) {
111				window.location.href = dropdownItem.getAttribute( 'href' );
112				closeDropdown();
113			} else {
114				var href = dropdownItem.getAttribute( 'data-href' );
115				window.open( href, '_blank', 'width=600,height=400,scrollbars=yes' );
116				closeDropdown();
117			}
118			return;
119		}
120
121		// Klik na przycisk share.
122		var shareBtn = e.target.closest( '[data-share-url]' );
123		if ( shareBtn ) {
124			e.preventDefault();
125			e.stopPropagation();
126
127			// Toggle — jeśli ten sam przycisk, zamknij.
128			if ( activeDropdown && activeDropdown.parentElement === shareBtn.parentElement ) {
129				closeDropdown();
130				return;
131			}
132
133			closeDropdown();
134
135			var url = shareBtn.getAttribute( 'data-share-url' );
136			var title = shareBtn.getAttribute( 'data-share-title' ) || '';
137
138			var dropdown = createDropdown( url, title );
139			shareBtn.parentElement.style.position = 'relative';
140			shareBtn.parentElement.appendChild( dropdown );
141
142			// Wymuszenie reflow przed dodaniem klasy active (animacja).
143			dropdown.offsetHeight; // eslint-disable-line no-unused-expressions
144			dropdown.classList.add( 'share-dropdown--active' );
145
146			activeDropdown = dropdown;
147			return;
148		}
149
150		// Klik poza — zamknij.
151		if ( activeDropdown && ! e.target.closest( '.share-dropdown' ) ) {
152			closeDropdown();
153		}
154	} );
155
156	// Zamknij na Escape.
157	document.addEventListener( 'keydown', function( e ) {
158		if ( e.key === 'Escape' ) {
159			closeDropdown();
160		}
161	} );
162}() );

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.