我制作了剪辑截图并上传了Firefox的插件。我想带一个允许用户发推特图片的功能。
我正在尝试自动执行步骤1-4。我可以完美地做1,2和4。
我正在尝试以编程方式执行第3步。似乎Twitter不使用input[type=file].files
html5 api,他们正在做一些自定义javascript。有谁知道我如何编程添加图像?我有完全privelaged javascirpt代码访问,因为这是一个Firefox插件。
我无法告诉用户附加信息的一个重要原因是这些图像存储在内存中以提高性能,而不是存储在文件中,因此我需要以编程方式附加它们。
它使用此处定义的mozSetFileArray
:https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement及其兄弟mozSetFileNameArray
:https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/mozSetFileNameArray
// Step 1 - Open twitter.com (for testing purposes, twitter.com is open in tab 7 so I dont load it here)
var aContentWindow = gBrowser.tabContainer.childNodes[6].linkedBrowser.contentWindow; // gets window of tab 7
var aContentDocument = aContentWindow.document;
// Step 2 - Open tweet modal
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();
// Step 3 - Attach two File instances for test purposes (in production will attach Blob)
var inputAddPhoto = aContentDocument.getElementById('global-tweet-dialog').querySelector('input[type=file]');
console.info('inputAddPhoto:', inputAddPhoto, inputAddPhoto.mozGetFileNameArray);
if (!inputAddPhoto) {
throw new Error('add photo button not found! i have no idea what could cause this');
}
var newFiles = [];
var myFile1 = new File('C:\\Users\\Vayeate\\Pictures\\Screenshot - Tuesday, August 11, 2015 6-33-58 AM.png'); // using local file for testing
var myFile2 = new File('C:\\Users\\Vayeate\\Pictures\\Screenshot - Tuesday, August 11, 2015 6-38-08 AM.png'); // using local file for testing
newFiles.push(myFile1);
newFiles.push(myFile2);
inputAddPhoto.mozSetFileArray(newFiles);
// Step 4 - Focus message field
// var richInputTweetMsg = aContentDocument.getElementById('tweet-box-global');
// richInputTweetMsg.focus(); // not needed because as when the modal opens twitter puts focus into here anways
// Step 5 - Done, now user can choose to type message, tag "whose in photo", and remove previews. The great thing is with this method, the images have not bothered twitter servers, the images are not uploaded until users final write off, when they click the "Tweet" button
我做了一些调试器检查,我在断点上放了一个断点,发现它出现在这里,有某种add
函数。