简体   繁体   English

在Greasemonkey脚本中使用jQuery强制换行?

[英]Forcing word-wrap with jQuery in a Greasemonkey script?

I'm writing a GreaseMonkey script that utilizes jQuery for a forum. 我正在编写一个将jQuery用于论坛的GreaseMonkey脚本。 I'm trying to force long words in posts to break, to avoid stretching the page. 我正试图强迫帖子中的长单词打乱,以免拉长页面。

Originally I had something like this: 本来我有这样的事情:

        $('.post').html(function(i, v) {
            var edited = false;
            // Break tags, links, and line breaks into separate 
            var words = v.replace(/<span(.+)<\/script\>/gi, " ").replace(/\</ig, " ").replace(/http:[^ ]+/gi, " ").replace(/&[^;]+;/gi, " ").replace(/[\n|\r]/g, " ").split(" ");
            for (var ii = 0; ii < words.length; ii++) {
                if (words[ii].length > word_length) {
                    edited = true;
                    v = v.replace(words[ii], insert_breaks(words[ii]));
                }
            }
            if (edited) return v;
        });

The site naturally has different elements within each post (eg bold tags, anchors, etc), so I stripped them out to leave only the text, which I then check for word length (defined earlier), and replace the word with its broken part. 该站点在每个帖子中自然具有不同的元素(例如,粗体标签,锚点等),因此我将其删除以仅保留文本,然后检查其字长(之前已定义),并用破损的部分替换了该字。 Some posts will contain some JavaScript, formatted like <span>...</span><script>...</script> , which is why I use that first replace that you see. 有些帖子将包含一些JavaScript,其格式如<span>...</span><script>...</script> ,这就是为什么我使用您看到的第一个替换内容的原因。

The issue I'm having is that, when a word is broken and I return v , the native JavaScript contained in the element is broken, and the GreaseMonkey script halts after replacing the HTML for the element. 我遇到的问题是,当一个单词损坏并返回v ,元素中包含的本机JavaScript损坏了,而GreaseMonkey脚本在替换元素的HTML之后暂停。

I don't see how I can use the CSS word-break , since I would need to set a width -- something I can't set for every user, and since I can't trust the width at page load. 我看不到如何使用CSS word-break ,因为我需要设置宽度-我无法为每个用户设置该宽度,并且由于我在页面加载时不信任该宽度。

Found a solution. 找到了解决方案。 The best way I found is to cycle through each node in the element, and if it's a text node, then modify the text from there. 我发现最好的方法是循环遍历元素中的每个节点,如果它是文本节点,则从那里修改文本。 Here's the gist of it: 这是要点:

    function insert_breaks(mtext) {
        if (mtext.length <= word_length) {
            return mtext;
        } else {
            mresult = mtext.substr(0, word_length) + " ";
            var newsection = mtext.substr(word_length);
            return mresult + insert_breaks(newsection);
        }               
    }

    function checkTextNodes(node) {
        if (node.nodeType == 3) {
            var v = node.nodeValue;
            var edited = false;
            var words = v.split(" ");
            for (var ii = 0; ii < words.length; ii++) {
                if (words[ii].length > word_length) {
                    edited = true;
                    v = v.replace(words[ii], insert_breaks(words[ii]));
                }
            }       
            if (edited) node.nodeValue = v; 
        } else {
            for (var i = 0; i < node.childNodes.length; i++) {
                checkTextNodes(node.childNodes[i]); 
            }
        }
    }

    function word_break() { 
        $('.post').contents().each(function() {
            checkTextNodes(this);
        });         
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM