Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

javascript - google pdf viewer search functionality

I have embedded a google pdf viewer in an iframe on my webpage. I want to build a functionality which will have a text box and a search button which will help in searching the word in the embedded pdf ONLY!.

activating ctrl+f through button click opens the window search functionality in chrome(It does not search words within the embedded pdf).

As I am using google pdf viewer, if I click on the scroll bar and then press ctrl+f then I am able to search using the google pdf viewer's search functionality.

So the main question is, How can I activate google pdf viewers search functionality on button click from outside the iframe.

Thanks

question from:https://stackoverflow.com/questions/65912698/google-pdf-viewer-search-functionality

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you are looking for is window.getSelection() method. This method returns a specific Selection object with the range of the selected text on the web page.

Here is how you can use onselectionchange() together with pdf.js

<script >

  // addEventListener version
  document.addEventListener('selectionchange', () => {
    console.log(document.getSelection());
  });

// onselectionchange version
document.onselectionchange = () => {
  var text = getSelectedText();

  if (text) {
    alert(text);

  }
};

function getSelectedText() {
  if (window.getSelection) {
    return window.getSelection().toString();
  } else if (document.selection) {
    return document.selection.createRange().text;
  }
  return '';
}

</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...