使用文件

时间:2015-08-12 02:03:57

标签: firefox-addon

用户通过删除文件将图像上传到此表单。

有没有办法以编程方式使用drop / File对象触发Blob?像MouseEvent.initMouseEvent() https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/initMouseEvent

这样的东西

但是像initDropEvent

我在这里研究:

基于此,我从暂存器中运行了这个代码,其中包含了twitter加载的标签,然后点击了" tweet"按钮:

// https://dxr.mozilla.org/mozilla-central/source/browser/base/content/test/newtab/head.js?offset=100#690
//https://dxr.mozilla.org/mozilla-central/source/browser/base/content/test/newtab/browser_newtab_bug765628.js#21

/**
 * Creates a custom drag event.
 * @param aEventType The drag event's type.
 * @param aData The event's drag data (optional).
 * @return The drag event.
 */
function createDragEvent(aContentWindow, aEventType, aData, aDataType) {
  // aDataType text/x-moz-url, text/plain, etc
  let dataTransfer = new (aContentWindow).DataTransfer("dragstart", false);
  dataTransfer.mozSetDataAt(aDataType, aData, 0);
  let event = aContentWindow.document.createEvent("DragEvents");
  event.initDragEvent(aEventType, true, true, aContentWindow, 0, 0, 0, 0, 0,
                      false, false, false, false, 0, null, dataTransfer);

  return event;
}

function sendTwitterDropEvent() {

    var aContentWindow = gBrowser.contentWindow;
    var aContentDocument = aContentWindow.document;

    var btnNewTweet = aContentDocument.getElementById('global-new-tweet-button');
    console.info('btnNewTweet:', btnNewTweet);
    if (!btnNewTweet) {
        throw new Error('global tweet button not found, probably not logged in');
    }

    btnNewTweet.click();

    var inputAddPhoto = aContentDocument.getElementById('global-tweet-dialog').querySelector('input[type=file]');
    if (!inputAddPhoto) {
        throw new Error('add photo button not found! i have no idea what could cause this');
    }

    var formTweet = aContentDocument.getElementById('global-tweet-dialog-dialog').querySelector('form'); //<form.t1-form.tweet-form has-preview has-thumbnail dynamic-photos photo-square-4>
    if (!formTweet) {
        throw new Error('tweet form not found! i have no idea what could cause this');
    }
    console.info('formTweet:', (formTweet instanceof Ci.nsIDOMNode));

    var ifaceReq = aContentWindow.QueryInterface(Ci.nsIInterfaceRequestor);
    var windowUtils = ifaceReq.getInterface(Ci.nsIDOMWindowUtils);

    var aDragData = 'site 99';
    var aDragDataType = 'plain/text';

    var event = createDragEvent(aContentWindow, "drop", aDragData, aDragDataType);
    windowUtils.dispatchDOMEventViaPresShell(formTweet, event, true);
}

sendTwitterDropEvent();

这应该使用&#34;网站99&#34;填充推文输入。但它没有。我正在使用纯文本进行测试,然后在我开始工作后,我正考虑转移到File / Blob

简化的非twitter测试用例

打开标签:data:text/html,<input id=rawr>

打开暂存器并从暂存器运行此代码:

function createDragEvent(aContentWindow, aEventType, aData, aDataType) {
  // aDataType text/x-moz-url, text/plain, etc
  let dataTransfer = new (aContentWindow).DataTransfer("dragstart", false);
  dataTransfer.mozSetDataAt(aDataType, aData, 0);
  let event = aContentWindow.document.createEvent("DragEvents");
  event.initDragEvent(aEventType, true, true, aContentWindow, 0, 0, 0, 0, 0,
                      false, false, false, false, 0, null, dataTransfer);

  return event;
}

function sendTwitterDropEvent() {

    var aContentWindow = gBrowser.contentWindow;
    var aContentDocument = aContentWindow.document;

    var formTweet = aContentDocument.getElementById('rawr');
    if (!formTweet) {
        throw new Error('tweet form not found! i have no idea what could cause this');
    }
    console.info('formTweet:', (formTweet instanceof Ci.nsIDOMNode), formTweet);

    var ifaceReq = aContentWindow.QueryInterface(Ci.nsIInterfaceRequestor);
    var windowUtils = ifaceReq.getInterface(Ci.nsIDOMWindowUtils);

    var aDragData = 'site 99';
    var aDragDataType = 'plain/text';

    var event = createDragEvent(aContentWindow, "drop", aDragData, aDragDataType);
    var rezDrop = windowUtils.dispatchDOMEventViaPresShell(formTweet, event, true);
    console.info('rezDrop:', rezDrop);

}

sendTwitterDropEvent();

0 个答案:

没有答案