当前位置: 首页 > 知识库问答 >
问题:

截断复制的web文本

阎裕
2023-03-14

此代码完全符合我的需求:将版权信息添加到复制的Web文本的底部。但是,它复制了整篇文章。我想限制它说50个字或200个字符。我应该如何继续?

$(document).ready(function () {
    document.body.oncopy = function () {
        var body_element = document.getElementsByTagName('body')[0];
        var selection;
        selection = window.getSelection();
        var pagelink = "<br />Read more at: <a href='" + document.location.href + "'>" + document.location.href + "</a><br />";
        var copytext = selection + pagelink;
        var newdiv = document.createElement('div');
        body_element.appendChild(newdiv);
        newdiv.innerHTML = copytext;
        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {
            body_element.removeChild(newdiv);
        }, 0);
    };
});

JSFIDDLE演示

不知道用< code>substring还是上面的代码没有用< code>write函数。

共有1个答案

龙兴学
2023-03-14

我在你的代码中添加了一些额外的指令。首先,从复制的文本中删除前200个字符(如果超过200个字符),其次,如果超过50个单词,则删除前50个单词。尝试更改< code>wordsLimit和< code>charactersLimit变量的值,以确定效果。

$(document).ready(function () {
    document.body.oncopy = function () {
    	var wordsLimit = 50;
        var charactersLimit = 200;
        var body_element = document.getElementsByTagName('body')[0];
        var selection;
        selection = window.getSelection();
        var limitChar = selection.toString().slice(0,charactersLimit);
		var limitWord = limitChar;
        var iter = 0;
        var end = null;
        limitWord.replace(/\s/g,function(a,b){
          iter++;
          if(iter===wordsLimit) limitWord = limitWord.slice(0,b);
        });
      
        var pagelink = "<br />Read more at: <a href='" + document.location.href + "'>" + document.location.href + "</a><br />";
        var copytext = limitWord + pagelink;
        var newdiv = document.createElement('div');
        body_element.appendChild(newdiv);
        newdiv.innerHTML = copytext;
        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {
            body_element.removeChild(newdiv);
        }, 0);
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam hendrerit orci vel urna tincidunt, id aliquet leo dapibus. Nunc sodales arcu auctor, aliquam augue ut, blandit lorem. Ut eleifend dui in interdum fringilla. Etiam eleifend, sem et varius ornare, massa tellus tincidunt metus, ac ultricies ex diam ac arcu. Pellentesque at scelerisque ex. Quisque lobortis lectus sit amet porttitor dapibus. Nunc eget sagittis enim. Aenean mollis rutrum ante. Etiam lacinia aliquam pellentesque. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam hendrerit orci vel urna tincidunt, id aliquet leo dapibus. Nunc sodales arcu auctor, aliquam augue ut, blandit lorem. Ut eleifend dui in interdum fringilla. Etiam eleifend, sem et varius ornare, massa tellus tincidunt metus, ac ultricies ex diam ac arcu. Pellentesque at scelerisque ex. Quisque lobortis lectus sit amet porttitor dapibus. Nunc eget sagittis enim. Aenean mollis rutrum ante. Etiam lacinia aliquam pellentesque. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam hendrerit orci vel urna tincidunt, id aliquet leo dapibus. Nunc sodales arcu auctor, aliquam augue ut, blandit lorem. Ut eleifend dui in interdum fringilla. Etiam eleifend, sem et varius ornare, massa tellus tincidunt metus, ac ultricies ex diam ac arcu. Pellentesque at scelerisque ex. Quisque lobortis lectus sit amet porttitor dapibus. Nunc eget sagittis enim. Aenean mollis rutrum ante. Etiam lacinia aliquam pellentesque. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam hendrerit orci vel urna tincidunt, id aliquet leo dapibus. Nunc sodales arcu auctor, aliquam augue ut, blandit lorem. Ut eleifend dui in interdum fringilla. Etiam eleifend, sem et varius ornare, massa tellus tincidunt metus, ac ultricies ex diam ac arcu. Pellentesque at scelerisque ex. Quisque lobortis lectus sit amet porttitor dapibus. Nunc eget sagittis enim. Aenean mollis rutrum ante. Etiam lacinia aliquam pellentesque. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
<p>This is the last line</p>
 类似资料:
  • 假设我创造了一个情节: 使用Ipython(通过Spyder)时,绘图显示ok。但是,当我查看保存的图像时,它显示如下: 如您所见,文本被截断。处理此问题的建议做法是什么? 我通过增加体形尺寸,然后重新调整尺寸来解决这个问题。然而,我的目标是生成一组文本大小一致的图像(图形大小不同);因此,这种方法并不理想。 笔记虽然存在类似的问题,但该问题的不同之处在于: 处理和

  • 我目前使用引导表(http://bootstrap-table.wenzhixin.net.cn/)与引导可编辑扩展(https://github.com/wenzhixin/bootstrap-table/tree/master/src/extensions/editable)创建一个表与可编辑的字段。我注意到,当一个字符串包含qoutes时,例如:这是一个“测试”,字符串在第一个“测试”中被截

  • 我有一个文本需要在定义的长度内被截断(比如14),我想在被截断文本的末尾添加一个省略号。 示例输出: 此处有一些文本。。。 这可以在CSS中完成吗?

  • 功能限制拦截断点 bp EnableMenuItem 禁止或允许菜单项 bp EnableWindow 禁止或允许窗口

  • 问题内容: 在Java中截断文件的最佳实践方法是什么?例如,此虚拟函数仅作为示例来阐明其意图: 由于该文件充当占位符,因此无法将其删除。 问题答案: 使用FileChannel.truncate:

  • 我想用句子截断文本。 示例文本:'Lorem ipsum dolor坐在amet,奉献adipiscing elit!UT车辆laoreet urna, commodo,在马萨诸塞州。赛德volutpat nunc简历urna拍卖,在tempus enim rhoncus。马蒂斯康莫多的莫尔比交流电击器?Morbi在ornare Arcu,sagittis scelerisque risus。Ae