1/* Tooltips */ 2const licences = { 3"all-rights-reserved": { 4 name: 'All rights reserved', 5 permalink: 'http://rightsstatements.org/vocab/InC/', 6 }, 7"cc_by": { 8 name: 'Attribution (CC BY)', 9 permalink: 'https://creativecommons.org/licences/by/', 10 }, 11"cc_by-sa": { 12 name: 'Attribution-ShareAlike (CC BY-SA)', 13 permalink: 'https://creativecommons.org/licences/by-sa/' 14 }, 15"cc_by-nd": { 16 name: 'Attribution-NoDerivs (CC BY-ND)', 17 permalink: 'https://creativecommons.org/licences/by-nd/' 18 }, 19"cc_by-nc": { 20 name: 'Attribution-NonCommercial (CC BY-NC)', 21 permalink: 'https://creativecommons.org/licences/by-nc/' 22 }, 23"cc_by-nc-sa": { 24 name: 'Attribution-NonCommercial-ShareAlike CC BY-NC-SA', 25 permalink: 'https://creativecommons.org/licences/by-nc-sa/' 26 }, 27"cc_by-nc-nd": { 28 name: 'Attribution-NonCommercial-NoDerivs CC BY-NC-ND', 29 permalink: 'https://creativecommons.org/licences/by-nc-nd/' 30 }, 31"cc_0": { 32 name: 'Public Domain Dedication CC ZERO', 33 permalink: 'https://creativecommons.org/publicdomain/zero/' 34 }, 35"public-domain": { 36 name: 'Public domain', 37 permalink: 'https://creativecommons.org/publicdomain/mark/' 38 }, 39} 40 41const licenceVers = { 42 "1.0": "1.0", 43 "2.0": "2.0", 44 "2.5": "2.5", 45 "3.0": "3.0", 46 "4.0": "4.0" 47} 48 49const tmpl = '<button class="unbutton toggle-button" aria-expanded="false" aria-controls="info-{{id}}"><div class="visibly-hidden">Image licence</div></button><small class="label" id="info-{{id}}" aria-hidden="true"><a href="{{sourceurl}}" target="_blank" class="source"><div class="name">{{title}}</div><div class="description">{{description}}</div></a><a class="licence" href="{{licenceurl}}" rel="licence" target="_blank"><img src="{{src}}" > {{author}}</a></small>'; 50 51function getRandomInt(min, max) { 52 min = Math.ceil(min); 53 max = Math.floor(max); 54 return Math.floor(Math.random() * (max - min) + min); 55} 56function addButtons(img) { 57 58 if( !img.getAttribute('data-id') ) return; 59 60 const top = (img.offsetTop); 61 const left = (img.offsetLeft + img.clientWidth); 62 const offsetParentWidth = (img.offsetParent.clientWidth); 63 64 let tooltip = img.parentNode.querySelector('.cc_tooltip'); 65 if(!tooltip) { 66 67 /** Licence */ 68 let licence = img.getAttribute('data-licence'); 69 if( !licence ) licence = ccTooltips.defaultLicence; 70 71 if( !( licence in licences ) ) return; 72 73 let licenceUrl = licences[licence].permalink 74 75 /** Licence ver */ 76 if( ! ( 'noVer' in licences[licence] ) ) { 77 78 let licenceVer = img.getAttribute('data-licencever'); 79 if( !licenceVer ) licenceVer = ccTooltips.defaultLicenceVer; 80 81 if( licenceVer in licenceVers ) { 82 licenceUrl = licenceUrl + licenceVer + '/'; 83 } 84 85 } 86 87 88 /** Template */ 89 const rand = getRandomInt( 100, 999 ); 90 91 tooltip = document.createElement('div'); 92 tooltip.innerHTML = tmpl 93 .replace(/{{sourceurl}}/g, ( !!img.getAttribute('data-source')) ? img.getAttribute('data-source') : '') 94 .replace(/{{title}}/g, ( !!img.getAttribute('data-title')) ? (img.getAttribute('data-title')) : '') 95 .replace(/{{description}}/g, ( !!img.getAttribute('data-description')) ? (img.getAttribute('data-description')) : '') 96 .replace(/{{licenceurl}}/g, licenceUrl) 97 .replace(/{{src}}/g, ccTooltips.imgUrl + licence + '.svg') 98 .replace(/{{author}}/g, ( !!img.getAttribute('data-author')) ? (img.getAttribute('data-author')) : '') 99 100 .replace(/{{id}}/g, (( !!img.getAttribute('data-id')) ? img.getAttribute('data-id') : '') + rand ) 101 //.replace(/{{licencename}}/g, ( licence in licences ) ? licences[licence].name : '') 102 //.replace(/{{licencever}}/g, ( !!img.getAttribute('data-licencever')) ? img.getAttribute('data-licencever') : '') 103 104 tooltip.classList.add('cc_tooltip') 105 img.parentNode.insertBefore(tooltip, img.nextSibling); 106 } 107 108 tooltip.style.top = top + 'px'; 109 tooltip.style.right = (offsetParentWidth - left) + 'px'; 110 tooltip.style.maxWidth = (img.clientWidth / 2) + 'px'; 111} 112function addTips(images) { 113 [].forEach.call( images, function(img) { 114 115 if(img.complete && img.naturalHeight !== 0) { 116 addButtons(img); 117 } else { 118 img.addEventListener("load", function(){ 119 addButtons(this) 120 }); 121 } 122 } ) 123} 124 125var imgs = document.querySelectorAll('.component-main-content img[class*="wp-image-"], .component-post-list img.wp-post-image, .component-post-header img.wp-post-image, .content-gallery img'); 126if(imgs) { 127 addTips(imgs) 128} 129window.addEventListener("resize", function() { 130 imgs = document.querySelectorAll('.component-main-content img[class*="wp-image-"], .component-post-list img.wp-post-image, .component-post-header img.wp-post-image, .content-gallery img'); 131 addTips(imgs) 132}); 133document.addEventListener("DOMContentLoaded", function() { 134 var observer = new MutationObserver(fnHandler), 135 elTarget = document.body, 136 objConfig = { 137 childList: true, 138 subtree : true, 139 attributes: false, 140 characterData : false 141 }; 142 143 observer.observe(elTarget, objConfig); 144 145 function fnHandler (mutationsList, observer) { 146 mutationsList.forEach(function(mutation) { 147 var added = mutation.addedNodes; 148 for (var i = 0; i < added.length; i++) { 149 imgs = added.item(i).querySelectorAll('.component-main-content img[class*="wp-image-"], .component-post-list img.wp-post-image, .component-post-header img.wp-post-image, .content-gallery img'); 150 if(imgs) { 151 addTips(imgs) 152 } 153 } 154 }); 155 } 156});
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.