2017-04-09 71 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); 
}); 
相關問題