阻止或检测 iframe 上的下载

huangapple go评论66阅读模式
英文:

Preventing or detecting download on iframe

问题

我有一个对我来说似乎很普遍的问题,但我找不到正确的解决办法。

我有一个通过iframe显示的文档。顶部有一个工具栏,最终用户可以从中下载文档。现在我想要记录关于下载的信息(所以我可能需要一些事件),或者禁用下载选项。

我知道我可以通过添加#toolbar=0来禁用整个工具栏,但最终用户可以自己更改它并仍然下载文档而不记录它,所以这对我来说不是一个合适的解决办法。

英文:

I have a problem which seemed common to me, but I couldn't find a right solution.

I have a document showing up via iframe. On the top there is a toolbar where the end user can download a document. Now I want to either log the information about downloading (so I need some event maybe) or disable the download option.

I know that I can disable whole toolbar by adding #toolbar=0, but the end user can just change it by himself and still download the document without logging it, so it is not a suitable solution for me.

答案1

得分: 1

如果您不一定要使用iframe,您可以改用embedobject标签,以防止出现工具栏。

对于特定的iframe,如果您想要禁用下载功能,可能会因为不同的浏览器将iframe转化为不同的元素来呈现PDF而需要特定于浏览器的解决方案。如果您确切知道您正在处理哪个浏览器,您可以尝试解绑下载按钮上的监听器,以下是我在Chrome中测试过的解决方案,似乎可以工作:

var old_element = document.getElementById("viewer").shadowRoot.getElementById("toolbar").shadowRoot.getElementById("downloads").shadowRoot.getElementById("download");
var new_element = old_element.cloneNode(true);
old_element.parentNode.replaceChild(new_element, old_element);

感谢Ben D为他们在此问题上的回答,您也可以将cloneNode和replaceChild步骤替换为addEventListener,如果您想进行日志记录。我认为这仍然会有问题,因为足够聪明/决心的用户仍然可以下载PDF(查看此文章的附加信息:https://www.w3docs.com/snippets/html/how-to-embed-pdf-in-html.html),因此根据您的最终目标,我建议为访客用户呈现PDF的静态图像预览,而完整的查看器可以限制为已登录的用户。

英文:

If you're not dead set on an iframe, you can use an embed or object tag instead to prevent such a toolbar from appearing.

With iframes specifically, if you want to disable download functionality you're probably going to end up with something browser specific, because different browsers translate iframes into different elements to actually render the PDF. If you know exactly what browser you're dealing with, you could try unbinding the listener from the download button, here is a solution that I tested in Chrome that seems to work:

var old_element = document.getElementById("viewer").shadowRoot.getElementById("toolbar").shadowRoot.getElementById("downloads").shadowRoot.getElementById("download");
var new_element = old_element.cloneNode(true);
old_element.parentNode.replaceChild(new_element, old_element);

Credits to Ben D for their answer to this question on listener removal, and you can replace the cloneNode and replaceChild steps with addEventListener if you want to go down the logging route. I think this will still end up with issues where a sufficiently savvy/determined user will be able to download the PDF though (check out this article additional info: https://www.w3docs.com/snippets/html/how-to-embed-pdf-in-html.html), so depending on your end-goal, I would recommend rendering a static image preview of the PDF for guest users, while the full viewer could be restricted to logged-in users.

答案2

得分: 1

PDF浏览器中的插件是用于在框架中嵌入对象的二进制插件,因此不能指望它们始终存在,如下所示。浏览器中的所有PDF都会首先下载,然后由客户端设备返回到嵌入的框架中。

而且作为二进制插件,它们不必允许JavaScript在其控件内部运行,因此即使您设置Toolbar=0或拷贝保护为“使用密码”,或者低质量打印禁止复制,这些都可以全部被忽略,除非扩展允许工具栏控件被尊重。

英文:

PDF viewers in a Browser are a Binary Plug In for object embed in frame so they never can be relied on to be present as shown below. ALL PDFs in a browser will download first then be returned by the client DEVICE to an imbedded frame.

Also as a binary plug-in they do not have to allow JavaScript inside their controls so even if you say Toolbar=0 or copy protection is "use password" or low quality print or no copying those can all be ignored unless the extender allows the toolbar control to be honoured.

阻止或检测 iframe 上的下载

huangapple
  • 本文由 发表于 2023年5月10日 20:49:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定