将文本从外部域 iFrame 拖动到父窗口中的可放置元素?

发布于 2024-11-04 14:35:12 字数 1385 浏览 0 评论 0原文

我正在构建一个 Web 应用程序,要求允许用户将文本从 iframe(src 可以来自任何外部域)拖动到父窗口中的可放置元素。虽然我可以轻松获取 iFrame 窗口的句柄,但当我尝试从 iFrame 中选择数据时,我收到了预期的安全错误。

目前,我正在研究从 Mark Kolich 网站上的帖子中复制的代码。我已将其修改为在 iFrame 中查找选定的文本。我尝试根据我在这里读到的内容修改 URL 片段(即,在 URL 末尾添加“#crunch”): http://softwareas.com/cross-domain-communication-with-iframes

...但这似乎没有什么区别。我还没有超越简单地尝试获取所选文本的范围。

有没有办法捕获 iFrame 内突出显示的文本(指向外部域),以便可以将其拖动到父窗口上的元素?

这是代码:

if(!window.crunch){
  crunch = {};
}

crunch.Selector = {};
crunch.Selector.getSelected = function(){

    var s = $("#xFrameWindow").attr('src');
    $("#xFrameWindow").attr('src', s + "#crunch")

    var t = '';

    if (window.frames["xFrameWindow"].getSelection) {
        t = window.frames["xFrameWindow"].getSelection();
    } else if(window.frames["xFrameWindow"].document.getSelection) {
        t = window.frames["xFrameWindow"].document.getSelection();
    } else if(window.frames["xFrameWindow"].document.selection) {
        t = window.frames["xFrameWindow"].document.selection.createRange().text;
    }

    return t;
}

crunch.Selector.mouseup = function(){
  var st = crunch.Selector.getSelected();
  if(st!=''){
    alert("You selected:\n"+st);
  }
}

$(document).ready(function(){
  $(document).bind("mouseup", crunch.Selector.mouseup);
});

I am building a web application with a requirement to allow users to drag text from an iframe (src could be from any external domain) to a droppable element in my parent window. While I can easily get a handle on the iFrame window, I get the expected security errors when I attempt to select data from within the iFrame.

Currently, I am playing around with code copied from a Mark Kolich post on his website. I have modified it to look for selected text in an iFrame. I have tried modifying the URL fragment (i.e., adding "#crunch" to the end of the URL) based on what I read here: http://softwareas.com/cross-domain-communication-with-iframes

... but it doesn't seem to make a difference. I haven't moved beyond simply trying to get hold of the selected text, yet.

Is there a way to capture text highlighted inside an iFrame (pointing to an external domain) so that it can be dragged to an element on the parent window?

Here is the code:


if(!window.crunch){
  crunch = {};
}

crunch.Selector = {};
crunch.Selector.getSelected = function(){

    var s = $("#xFrameWindow").attr('src');
    $("#xFrameWindow").attr('src', s + "#crunch")

    var t = '';

    if (window.frames["xFrameWindow"].getSelection) {
        t = window.frames["xFrameWindow"].getSelection();
    } else if(window.frames["xFrameWindow"].document.getSelection) {
        t = window.frames["xFrameWindow"].document.getSelection();
    } else if(window.frames["xFrameWindow"].document.selection) {
        t = window.frames["xFrameWindow"].document.selection.createRange().text;
    }

    return t;
}

crunch.Selector.mouseup = function(){
  var st = crunch.Selector.getSelected();
  if(st!=''){
    alert("You selected:\n"+st);
  }
}

$(document).ready(function(){
  $(document).bind("mouseup", crunch.Selector.mouseup);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文