我正試圖在使用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太
嘗試使用'$(「span」)。html(「」)。html($(「span」)。html()。split(「
」)[1])' –
我不需要對於所提到的頁面的解決方案,但對於存在一些隱藏文本的所有情況 – mrnfrancesco
如何避免代碼知道其「隱藏」 –