Skip to content

Instantly share code, notes, and snippets.

@aronwoost
Created July 30, 2012 19:43
Show Gist options
  • Save aronwoost/3209510 to your computer and use it in GitHub Desktop.
Save aronwoost/3209510 to your computer and use it in GitHub Desktop.
Pseudo example code for saving text fragments in HTML (or epubs)
/*
Looking for "and".
Markup is the following:
<body>
<div id="text">Pride and Prejudice</div>
</body>
*/
var range = {
startContainer:$("text"),
startOffset:6,
endContainer:$("text"),
endOffset:9
}
/*
Looking for "Prejudice is a book".
Markup is the following:
<body>
<div id="headline">Pride and Prejudice</div>
<div id="text">is a book, that is very popular</div>
</body>
*/
var range = {
startContainer:$("headline"),
startOffset:10,
endContainer:$("text"),
endOffset:9
}
range.getBoundingClientRect();
/*
Looking for "and".
Markup is the following:
<body>
<div id="text">Pride and Prejudice</div>
</body>
*/
var highlight = {
text:"and",
startContainer:"//*[@id='text']",
startOffset:6,
endContainer:"//*[@id='text']",
endOffset:9
}
/*
Looking for "and".
Markup is the following:
<body>
<div id="text">Pride and Prejudice</div>
</body>
*/
var highlight = {
text:"and",
startContainer:"/html/body/node()[1]",
startOffset:6,
endContainer:"/html/body/node()[1]",
endOffset:9
}
/*
Looking for "and".
Markup is the following:
<body>
<div id="text">Pride and Prejudice</div>
</body>
*/
var highlight = {
text:"and",
startContainer:"0/0",
startOffset:6,
endContainer:"0/0",
endOffset:9
}
/*
Looking for "Prejudice".
Markup is the following:
<body>
<div>
<div>A classic:</div>
<div>Pride <span>and</span> Prejudice</div>
</div>
</body>
*/
var highlight = {
text:"Prejudice",
startContainer:"0/1/2",
startOffset:1,
endContainer:"0/1/2",
endOffset:10
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment