Javascript在复制上附加链接

时间:2016-02-10 07:15:53

标签: javascript jquery

我在我的博客中添加了Javascript Append链接副本,意味着当有人从我的网站复制文本时,它还会复制博客文章网址并添加阅读更多标记,所以我想修改这个脚本,并使它像,当有人复制最少20个字符时,则此代码将复制读取更多标签在剪贴板中,如果用户复制少于20个字,则此脚本不起作用。所以,如果有人能够实现它,那将是非常有帮助的。 感谢

原始剧本 -

<script type="text/javascript">
function addLink() {
    var body_element = document.getElementsByTagName('body')[0];
    var selection;
    selection = window.getSelection();
    var pagelink = "<br /><br /> Read more at: <a href='"+document.location.href+"'>"+document.location.href+"</a><br />"; // change this if you want
    var copytext = selection + pagelink;
    var newdiv = document.createElement('div');
    newdiv.style.position='absolute';
    newdiv.style.left='-99999px';
    body_element.appendChild(newdiv);
    newdiv.innerHTML = copytext;
    selection.selectAllChildren(newdiv);
    window.setTimeout(function() {
        body_element.removeChild(newdiv);
    },0);
}
document.oncopy = addLink;
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</script>

1 个答案:

答案 0 :(得分:-2)

试试这个。只要选择的长度小于20,该函数将返回选择。

(* each record contains 4 values : there are as many records as you need
  Sub : the name of the subject
  EB  : the URL or filename of the ebook. if empty no ebook
  Fol : the name of the folder from Contents/Resources to be open. if empty,no folder to open
  EBApp : the application to be used to open EB. 
          In this exemple Maths uses "Preview", Drama uses texEdit and all others are using Safari
*)
global myRules -- to be use also in sub-routines

-- fill here all your definitions URLs and documents
set myRules to {{Sub:"Maths", EB:"maths_ebook.pdf", Fol:"Maths", EBApp:"Preview"}, {Sub:"History", EB:"www.google.com", Fol:"History", EBApp:"Safari"}}
set myRules to myRules & {{Sub:"Science", EB:"myURL", Fol:"Maths", EBApp:"Safari"}, {Sub:"French", EB:"www.apple.com", Fol:"French", EBApp:"Safari"}}
set myRules to myRules & {{Sub:"Drama", EB:"Drama.txt", Fol:"", EBApp:"TextEdit"}}


set R to display dialog "What subject would you like to access?" default answer "" buttons {"Open folder", "Open ebook", "Cancel"}
set theSubject to Capital(text returned of R) -- make sure 1 letter capital like in subjectsWithEbook
set theChoice to (button returned of R)
set NumItem to ItemList(theSubject)

if NumItem is 0 then -- don't know what to do with this subject not in the list !
display alert "Sorry; Subject " & theSubject & " is not available"
return
end if

set mySubject to item NumItem of myRules
if (theChoice is "Open folder") and (Fol of mySubject is not "") then
tell application "Finder" to open folder ((path to me as string) & "Contents:Resources:Subjects:" & (Fol of mySubject))
end if

if (theChoice is "Open ebook") and (EB of mySubject is "") then
display alert "Sorry; Subject " & theSubject & " has no ebook version available"
return
end if

if (theChoice is "Open ebook") and (EB of mySubject is not "") and (EBApp of mySubject is not "") then
if EBApp of mySubject is "Safari" then
    tell application "Safari" to open location (EB of mySubject)
else
    tell application (EBApp of mySubject) to open (EB of mySubject)
end if
end if

on ItemList(LSubject) -- return the item number of the subject in myRules
set LItem to 0
repeat with I from 1 to (count of myRules)
    if LSubject = Sub of item I of myRules then set LItem to I
end repeat
return LItem
end ItemList

on Capital(Localtext) -- set 1st letter in capital and other small caps.
set Letters to every character of Localtext
repeat with I from 1 to count of Letters
    set A to ASCII number of (item I of Letters)
    if (I = 1) and (A > 96) and (A < 123) then set A to A - 32
    if (I > 1) and (A > 64) and (A < 91) then set A to A + 32
    set item I of Letters to ASCII character of A
end repeat
return Letters as string
end Capital