用JavaScript替换选中的文本(在剪贴板上)

时间:2014-09-20 05:33:09

标签: javascript jquery

我有一个WP博客,我试图在我的国家做两个主要的在线期刊。他们有一个JS / JQuery代码,它具有以下功能:当用户尝试复制任何新闻的引用时,粘贴的内容将替换为"要共享此内容,请使用链接... "

我真的不太了解编程,但我发现了两个网站的代码,如下:

function disableCopy(){
var s, r;
var copyright = 'O material jornalístico produzido pelo Estadão é protegido por lei. Para compartilhar este conteúdo, utilize o link:<a href="'+window.location.href+'">'+window.location.href+'</a>';
var status = buscaCookie();

if(!status){
    $('.news').bind('copy', function () {
        //cria um span temporario se não existir
        if(!$("#tempbox").length)
            $('<span>').attr('id','tempbox').appendTo('body').css('font-size','0');

        var tempbox = document.getElementById ("tempbox");
        $('#tempbox').html(copyright);
         if (typeof window.getSelection !== 'undefined') {
            s  = window.getSelection();
            s.removeAllRanges();
            s.selectAllChildren(tempbox);
            r  = s.getRangeAt(0);

        }else{  
            s = document.selection.createRange();
            document.selection.empty();
            s.moveToElementText( tempbox) ;
            r = s.pasteHTML(copyright);
            s.select();
        }       
    });
}

}

$.fn.disable_copy = ( function ( $, window ) {
var enabled = true ,
    config = {
        message: 'Para compartilhar esse conteúdo, por favor utilize o link {{link}} ou as ferramentas oferecidas na página. Textos, fotos, artes e vídeos da Folha estão protegidos pela legislação brasileira sobre direito autoral. Não reproduza o conteúdo do jornal em qualquer meio de comunicação, eletrônico ou impresso, sem autorização da Folhapress (pesquisa@folhapress.com.br). As regras têm como objetivo proteger o investimento que a Folha faz na qualidade de seu jornalismo. Se precisa copiar trecho de texto da Folha para uso privado, por favor logue-se como assinante ou cadastrado.' ,
        copybox: '.short-cut'
    } ,
    base = {
        $el: null ,
        opts: {} ,
        // impede cópia para browsers que aceitem window.getSelection
        handle_copy: function ( el ) {
            var sel = window.getSelection() ,
                range = sel.getRangeAt(0) ;

            sel.removeAllRanges() ;
            sel.selectAllChildren( el ) ;

            return $.proxy( function () {
                sel.removeAllRanges() ;
                sel.addRange( range ) ;
            } , this ) ;
        } ,
        // fallback para IE lt 9
        handle_copy_alt: function ( el ) {
            var sel = document.selection && document.selection.createRange() ,
                bm = sel.getBookmark() ;

            sel.moveToElementText( el ) ;
            sel.select() ;

            return function () {
                sel = document.selection.createRange() ;
                sel.moveToBookmark( bm ) ;
                sel.select() ;
            } ;
        } ,
        // handle copy action
        copy: function ( e ) {
            var $msg ,
                reset ;

            if ( ! enabled ) {
                return ;
            }

            // ignore shortcut
            if ( $( e.target ).attr( 'type' ) === 'text' && $( e.target ).closest( this.opts.copybox ).length > 0 ) {
                return ;
            }

            // create temporary box
            $msg = $( document.createElement( 'span' ) ).css( 'text-indent', '-9999px' ).css( 'width', '1px' ).css( 'height', '1px' ) ;
            // set content
            $msg.text( this.opts.message.replace( '{{link}}', window.location.href.replace( "staging." , "" ) ) ) ;
            this.$el.append( $msg ) ;

            if ( typeof window.getSelection !== 'undefined' ) {
                reset = this.handle_copy( $msg.get(0) ) ;
            } else {
                reset = this.handle_copy_alt( $msg.get(0) ) ;
            }

            // remove temporary box
            window.setTimeout( function () {
                reset() ;
                $msg.remove() ;
            } , 0 ) ;
        }
    } ;

// Clipboard - Copyright
if ( folha.information.context.get() != "staging" ) {
    if ( $.fn.disable_copy ) {
        $( "#news .content" ).disable_copy() ;
    }
}

如果有人可以帮我在JSFiddle上创建一个替换从特定div / p / article中选择的文本的脚本,我将非常感激!

0 个答案:

没有答案
相关问题