I am trying to create a div tag to cover an embed tag, and did a little javascript so user can not right click the pdf inside embed tag to download it (I know it is useless, I doing this because my customer requested it). And this is the main part:
I want user still be able to scroll to pdf.
I programmed the scroll so that everytime user scroll, the div tag mentioned above will disappear, SO THAT USERS CAN SCROLL THROUGH THE PDF. But as soon as they stop scrolling, it will appear again, to prevent user from right click the pdf.
This is my code so far:
var wheeling;
window.addEventListener('wheel', function (e) {
if (!wheeling) {
console.log('start wheeling!');
document.querySelector("#lesson_pdf").style.display = "none";
}
clearTimeout(wheeling);
wheeling = setTimeout(function() {
console.log('stop wheeling!');
document.querySelector("#lesson_pdf").style.display = "block";
wheeling = undefined;
}, 300);
});
My html:
<div>
<div style="position: relative;">
//the embed tag I want to cover
<embed src=" {{ getImageFile($pdf_src) }}" class="tw-w-full tw-h-[500px] pdf-reader-frame" style="position: absolute;">
</div>
//The div tag I mentioned
<div style="position: absolute;
height: 100%;
width: 100%;
display: block;
z-index: 1;" id="lesson_pdf">
</div>
</div>
The thing is, it doesn’t work as expected. I sometimes have to scroll a lot of time before be able to scroll through the pdf file. Can somebody help me with this?
3
Answers
If your goal is to "to prevent user from right click the pdf", why not just disable the context menu? You can do that using the contextmenu event. It will be more efficient than displaying and hiding a div overlay (unless that was a super specific client request?)
I hope below code will help you.don’t forget to add the css
From what I understand, your goal ultimately is to prevent the user from downloading the pdf.
It had been covered in this answer on the question
How to disable right click on embed tag?
The answer links to an answer that disables context menu (The right click menu) on the div that contains the embed element, this essentially let the user unable to right click on the embed tag.
Disable right click on pdf inside embed element
Hopes this helps!