2017-04-09 70 views
1

我一直在为Greasemonkey编写这个脚本。问题是.tolowercase()显然它使任何大写的小写,这会破坏URL的。我研究过.startswith()和.endswith()作为可能的解决方案,例如以http和https开头;也许前缀检测一些'http *'?不管怎么说,这是下面的代码,在此先感谢:.tolowercase()仅包含除URL之外的文本?

// ==UserScript== 
// @name  twitter intent 
// @namespace random 
// @description twitter intent popupwindow text replace 
// @include  https://twitter.com/intent/* 
// @version  1 
// @grant  none 
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js 
// ==/UserScript== 


$('#status').each(function() { 
    var text = $(this).text(); 
    $(this).text(text.toLowerCase() 
     .replace('... ', ' ... ') 
     .replace('health ', '#health ') 
     .replace('video', 'Video') 
     .replace('emergency', '#emergency') 
     .replace('climate change', '#climatechange') 
     .replace('climate ', '#climate ') 
    );  
}); 

这是代码之前,HTML是跑就可以了。

<textarea id="status" name="status" required="" autofocus="" aria-required="true" aria-describedby="post-error char-count">EPA shuts down program helping states adjust to climate change http://hill.cm/FH2iWpq</textarea> 

编辑:它的重要,除了网址,文字是有忽略,如果检测到的话他们的自我,但仍.replace文字大写或小写。

+0

所以,你要的网址留下来,因为它是什么? –

+0

你是对的 – computerguy

+0

'$('#status')。each('没有意义,因为ID的定义必须是唯一的,也可以是URL中的文字或''标签?提供[mcve] – charlietfl

回答

1

所以我尝试了放置位置,网址和重新添加它的组合。

这落得这样的:

$('#status').each(function() { 
 
    var text = $(this).text(); 
 
    var n = text.indexOf('http'); 
 
    var url = text.match('http(s?):\/\/[^<\s]*'); 
 
    if(url){ 
 
     text = text.replace(url[0],'') 
 
    } 
 
    text = text.toLowerCase() 
 
     .replace('... ', ' ... ') 
 
     .replace('health ', '#health ') 
 
     .replace('video', 'Video') 
 
     .replace('emergency', '#emergency') 
 
     .replace('climate change', '#climatechange') 
 
     .replace('climate ', '#climate ') 
 
    if(url){ 
 
     text = text.slice(0,n) + url[0] + text.slice(n); 
 
    } 
 
    $(this).text(text); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<textarea id="status" name="status" required="" autofocus="" aria-required="true" aria-describedby="post-error char-count">EPA shuts down program helping states adjust to climate change http://hill.cm/FH2iWpq</textarea>

+0

感谢大家和他们的努力,但这个答案适合并且效果最好,我向前看给每个人的回复有利于我以外的人 – computerguy

+0

即时获取一个奇怪的问题,当使用'.replace('气候变化','#climatechange')'它删除了空间#climatechange和链接 – computerguy

+0

我修改了'var url = text.match('http(s?):\/\/[^ <\ s] *');'似乎可行,但我不知道它是否是最好的解决方案;我在http前面添加了一个空格。 – computerguy

1

尝试解析您的文字,网址,如果它的工作原理,不低的情况下它:

你可以看到它在我做了一个小提琴 - https://jsfiddle.net/y1jh5w0w/

text = text.split(' '); 

text.forEach(function (value, index) { 
    try { 
     var url = new URL(value); 
    } catch (ex) { 
     text[index] = value.toLowerCase(); 
    } 
}); 

text = text.join(' '); 
+0

好的,请尝试这个。 – Matansh

+0

即时通讯正在寻找替代多个单词如上所述,而不是一个特定的句子,如果这是你暗示的 – computerguy

+0

我可以添加它的重要忽略文本无论是大写还是小写,只忽略URLS – computerguy

0

为什么你不能使用正则表达式来找到,如果一些字符串从https://http://开始,那么不就可以申请toLowerCase

const regex = /^(https:\/\/)/g; 
 
const str = `https://www.FaceBook.com`; 
 
let m; 
 

 
while ((m = regex.exec(str)) !== null) { 
 
    // This is necessary to avoid infinite loops with zero-width matches 
 
    if (m.index === regex.lastIndex) { 
 
     regex.lastIndex++; 
 
    } 
 
    
 
    // The result can be accessed through the `m`-variable. 
 
    m.forEach((match, groupIndex) => { 
 
     console.log(`Found match, group ${groupIndex}: ${match}`); 
 
    }); 
 
}

所以,你可以创建一个函数来检查,如果字符串URL,然后在非URL字符串执行toLowerCase

让我知道这是你在找什么。

更新OP表现出一些片段后

我仍然相信正则表达式是你

这里的解决方案是更新后的片段

var str = 'EPA shuts down program helping states adjust to climate change http://hill.cm/FH2iWpq Which url is lowercased dummy text'; 
 
var regex = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/g; 
 
str.split(" ").forEach(function (obj) { 
 
    if(obj.match(regex) === null) { 
 
    obj = obj.toLowerCase(); 
 
    } 
 
    console.log(obj); 
 
})

正如你所看到的,除了URL之外,一切都变得更小。

+0

我想让它工作,但我没有任何运气 – computerguy

+0

ive添加这下面我的代码,与我的代码中的.tolowercase()结合起来,似乎工作。 – computerguy

+0

我可以添加其重要的忽略文本是否大写或小写,并且只忽略URLS,意味着如果文本是大写或更低,则文本本身应该只被检测到并被替换,如果这样说的话。 – computerguy

0
var dictionary1= { 
    " A":" b", 
    " B":" b", 
    " C":" C", 
}; 

jQuery(document).ready(function() { 
    setTimeout(function() { 
$("#status").each(function(){ 
    for(var ptrn in dictionary1){ 
     $(this).text($(this).text().replace(new RegExp(ptrn ,"g"), dictionary1[ptrn])); 
    } 
}); 
}, 100); 
}); 
相关问题