2017-07-27 67 views
1

我正試圖在使用javascript的HTML頁面中獲取選定的文本。 window.getSelection().toString()適用於所有情況,但只有一種。使用JavaScript獲取可見到用戶選定的文本

假設有一個HTML頁面下面的代碼片段:

Please copy the following text into your terminal to checkout my newest open source project: 
 

 
<code> 
 
    git clone 
 
    <span style="position: absolute; left: -100px; top: -100px"> /dev/null; some-evil-commands; <br/>git clone </span> 
 
    https://github.com/some-user/some-repository 
 
</code>

由於position: absolute; left: -100px; top: -100px含有惡猛砸span標籤命令是對用戶是不可見的,但如果他還是選擇/她選擇可見的文字。現在

window.getSelection().toString()導致

git clone /dev/null; some-evil-commands; git clone https://github.com/some-user/some-repository 

我需要知道,如果有一種方法可以讓我只得到選定的文本可見用戶;在這種情況下,我只需要

git clone https://github.com/some-user/some-repository 

N.B:這是確定使用jQuery太

+0

嘗試使用'$(「span」)。html(「」)。html($(「span」)。html()。split(「
」)[1])' –

+0

我不需要對於所提到的頁面的解決方案,但對於存在一些隱藏文本的所有情況 – mrnfrancesco

+0

如何避免代碼知道其「隱藏」 –

回答

-1

嘗試使用

<span style="color:rgba(0,0,0,0);font-size:0.5px;"> 

希望它能幫助。請注意:如果你的bash命令是「邪惡的」,請不要欺騙人們做他們不想要的事情。

+0

請閱讀問題。我不想知道如何使跨度可見,但如何在JavaScript中選擇只有用戶選擇的文本,並且他/她可見 – mrnfrancesco

+0

,所以您希望選擇整個事物,但它只顯示用戶克隆部分? –

+0

當用戶加載頁面時,他/她只會看到「git clone https:// ...」部分,因此如果用戶選擇他/她希望從選擇中獲得他/她看到的部分,隱藏部分被排除。 我想使用js將用戶可見的選定文本刪除隱藏部分,而不管它隱藏的方式 – mrnfrancesco

相關問題