“MediaWiki:Gadget-NotifyWhenCopy.js”的版本间的差异
来自小鱼君和他的朋友们
小 (//InPageEdit) 标签:InPageEdit |
|||
第45行: | 第45行: | ||
}); | }); | ||
// End | // End | ||
+ | }); | ||
+ | $('body').on('copy', function (e) { | ||
+ | if (typeof window.getSelection == 'undefined') return; //IE8 or earlier... | ||
+ | |||
+ | var body_element = document.getElementsByTagName('body')[0]; | ||
+ | var selection = window.getSelection(); | ||
+ | |||
+ | //if the selection is short let's not annoy our users | ||
+ | if (('' + selection).length < 100) return; | ||
+ | |||
+ | //if loged in as admin | ||
+ | //if () return; | ||
+ | |||
+ | //create a div outside of the visible area | ||
+ | //and fill it with the selected text | ||
+ | var newdiv = document.createElement('div'); | ||
+ | newdiv.style.position = 'absolute'; | ||
+ | newdiv.style.left = '-99999px'; | ||
+ | body_element.appendChild(newdiv); | ||
+ | newdiv.appendChild(selection.getRangeAt(0).cloneContents()); | ||
+ | |||
+ | //we need a <pre> tag workaround | ||
+ | //otherwise the text inside 'pre' loses all the line breaks! | ||
+ | if (selection.getRangeAt(0).commonAncestorContainer.nodeName == 'PRE') { | ||
+ | newdiv.innerHTML = '<pre>' + newdiv.innerHTML + '</pre>'; | ||
+ | } | ||
+ | |||
+ | newdiv.innerHTML += '<br/><br/>以上内容来自:<a href="'+location.href+'">'+location.href+'</a> (小鱼君和他的朋友们)<br/>除非另有声明,本网站所有文字内容采用<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh_CN">知识共享(Creative Commons) 署名-非商业性使用-相同方式共享 4.0</a>授权,转载请注明出处!。'; | ||
+ | |||
+ | selection.selectAllChildren(newdiv); | ||
+ | window.setTimeout(function () { body_element.removeChild(newdiv); }, 200); | ||
}); | }); |
2019年9月29日 (日) 13:13的版本
$(function() { // Start (new mw.Api()).get({ action: 'query', meta: 'allmessages', ammessages: 'License-description|Copy-notify|Copy-notify/page/'+wgPageName, amlang: mw.config.get('wgUserLanguage') || mw.config.get('wgContentLanguage') }).done(function(data) { // Variables var licenseDescriptionMsg = data.query.allmessages[0]['*'], customMsg = data.query.allmessages[1]['*'], customPageMsg = data.query.allmessages[2]['*'], defaultMsg = '你刚刚复制了<span style="font-weight:bold;">' + wgSiteName + '</span>上的内容!'+ licenseDescriptionMsg + '<br/>转载时请注明出处:<span style="font-style:italic;font-weight:bold;">' + decodeURIComponent(location.href) + '</span>', finalMsg; // Get custom messages if ( customPageMsg !== undefined && customPageMsg !== '-' && customPageMsg !== '' ) { finalMsg = customPageMsg .replace( /\$sitename/ig , wgSiteName ) .replace( /\$url/ig , decodeURIComponent(location.href) ) .replace( /\$license/ig , licenseDescriptionMsg ); } else if ( customMsg !== undefined && customMsg !== '-' && customMsg !== '' ) { finalMsg = customMsg .replace( /\$sitename/ig , wgSiteName ) .replace( /\$url/ig , decodeURIComponent(location.href) ) .replace( /\$license/ig , licenseDescriptionMsg ); } else { finalMsg = defaultMsg; } var debugMsg = 'defaultMsg is:<br/>' + defaultMsg + '<hr/>customMsg is:<br/>' + customMsg + '<hr/>customPageMsg is:<br/>' + customPageMsg + '<hr/>finalMsg is:<br/>' + finalMsg; // Output $(window).on('copy',function() { $.toast({ text: finalMsg, icon: 'warning', hideAfter: 5000, position: 'top-left', //loaderBg: '#33a0fb' }) }); }); // End }); $('body').on('copy', function (e) { if (typeof window.getSelection == 'undefined') return; //IE8 or earlier... var body_element = document.getElementsByTagName('body')[0]; var selection = window.getSelection(); //if the selection is short let's not annoy our users if (('' + selection).length < 100) return; //if loged in as admin //if () return; //create a div outside of the visible area //and fill it with the selected text var newdiv = document.createElement('div'); newdiv.style.position = 'absolute'; newdiv.style.left = '-99999px'; body_element.appendChild(newdiv); newdiv.appendChild(selection.getRangeAt(0).cloneContents()); //we need a <pre> tag workaround //otherwise the text inside 'pre' loses all the line breaks! if (selection.getRangeAt(0).commonAncestorContainer.nodeName == 'PRE') { newdiv.innerHTML = '<pre>' + newdiv.innerHTML + '</pre>'; } newdiv.innerHTML += '<br/><br/>以上内容来自:<a href="'+location.href+'">'+location.href+'</a> (小鱼君和他的朋友们)<br/>除非另有声明,本网站所有文字内容采用<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh_CN">知识共享(Creative Commons) 署名-非商业性使用-相同方式共享 4.0</a>授权,转载请注明出处!。'; selection.selectAllChildren(newdiv); window.setTimeout(function () { body_element.removeChild(newdiv); }, 200); });