Hallo,
ich bräuchte da mal eure Hilfe für eine Firefox Erweiterung. Ich will verschiedene Dateitypen an Google Docs schicken.
Bisher schaut mein Code so aus:
Code
var GoogleDocs = {
onLoad: function() {
document.getElementById("contentAreaContextMenu")
.addEventListener("popupshowing", function(event) { GoogleDocs.onPopupShowing(event); }, false);
},
onPopupShowing: function(event) {
document.getElementById("context-Google-Docs")
.hidden = document.getElementById("context-openlinkintab").getAttribute("hidden");
},
};
function execute(event) {
if(!gContextMenu.onLink)return;
var url = gContextMenu.getLinkURL();
// retrieve the url from a google link
var googleURL = /^http:\/\/[a-z]+\.google\.[a-z]+\/url\?.*[\\?&]url=([^&#]+)/i;
// special case for yahoo search results
var yahooURL = /^http:\/\/[a-z]+\.yahoo\.[a-z]+\/.*\/\*\*([^&#]+)/i;
if(url.match(googleURL) || url.match(yahooURL)) {
url = decodeURIComponent(RegExp.$1);
}
getToken(url)
//openDoc(url);
}
function openDoc(aURI) {
var uploadURL = googleDocsURL + encodeURIComponent(aURI);
var prefManager = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var prefvalue = prefManager.getCharPref("sendtogoogledocs.whereToOpenUrl");
switch(prefvalue) {
case "openNewTab":
gBrowser.addTab(uploadURL);
break;
case "selectedNewTab":
gBrowser.selectedTab = gBrowser.addTab(uploadURL);
break;
case "openNewWindow":
window.open(uploadURL);
break;
case "openSameTab":
gBrowser.loadURI(uploadURL);
break;
}
}
function getToken(aURL) {
var xhr = new XMLHttpRequest();
var email = ""; //zum testen
var password = ""; //zum testen
xhr.open("POST", "https://www.google.com/accounts/ClientLogin?accountType=HOSTED_OR_GOOGLE&Email="+email+"&Passwd="+password+"&service=writely&source=GoogleDocs", true);
xhr.onreadystatechange = function(aEvent) {
if(xhr.readyState == 4) {
if(xhr.status == 200) {
var a = xhr.responseText.replace(/\n/g, "");
_AUTH = a.substr(a.indexOf("Auth=") + 5);
sendDoc(_AUTH, aURL);
} else {
alert(xhr.statusText)
}
}
}
xhr.send(null);
}
// var pattern = /<input type="hidden" name="security_token" value="([^"]+)">/;
// var matches = xhr.responseText.match(pattern);
// if(matches != null) { // retrieved security token successfully
// sendDoc(matches[1], aURL);
// } else { // failed to retrieve security token - not logged in?
// showAlert("Login Error", "Please login first!");
// gBrowser.selectedTab = gBrowser.addTab("https://docs.google.com");
// return;
// }
// } else { // server error
// showAlert("Error", "Error getting token!");
// return;
function sendDoc(aToken, aURL) { // http://code.google.com/intl/en/apis/desktop/articles/e9.html
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://docs.google.com/feeds/documents/private/full");
//document.getElementById("upload_txt").style.display = "";
// document.getElementById("upload_txt").value = "Uploading: " + data.name;
const BOUNDARY = "END_OF_PART";
const MULTI = "@mozilla.org/io/multiplex-input-stream;1";
const FINPUT = "@mozilla.org/network/file-input-stream;1";
const STRINGIS = "@mozilla.org/io/string-input-stream;1";
const BUFFERED = "@mozilla.org/network/buffered-input-stream;1";
const nsIMultiplexInputStream = Components.interfaces.nsIMultiplexInputStream;
const nsIFileInputStream = Components.interfaces.nsIFileInputStream;
const nsIStringInputStream = Components.interfaces.nsIStringInputStream;
const nsIBufferedInputStream = Components.interfaces.nsIBufferedInputStream;
/* this._uploadHistory.push({
"file": data.name,
"status": false,
"error": ""
});*/
//console.log(data.data);
var buf = Components.classes[STRINGIS].createInstance(nsIStringInputStream);
buf.setData(aURL, aURL.length); //<-- hier ist der fehler
// buf = buf.replace(/>.*<\/a>/gi,">");
//console.log(data.data.value.toString().length);
//alert(data.data.value)
//<a href="http://www.pgts.com.au/download/txt/wav.txt">wav.txt</a>
var mis = Components.classes[MULTI].createInstance(nsIMultiplexInputStream);
//var fin = Components.classes[FINPUT].createInstance(nsIFileInputStream);
//fin.init(file, 1, 292, null);
//var buf = Components.classes[BUFFERED].createInstance(nsIBufferedInputStream);
//buf.init(fin, (file.fileSize - 1));
var mime_1 = Components.classes[STRINGIS].createInstance(nsIStringInputStream);
var mime_1_string = new String();
mime_1_string += "\r\n";
mime_1_string += "Media multipart posting\r\n";
mime_1_string += "\r\n--" + BOUNDARY + "\r\n";
mime_1_string += "Content-Type: application/atom+xml\r\n";
mime_1_string += "\r\n";
mime_1_string += '<?xml version=\'1.0\' encoding=\'UTF-8\'?><atom:entry xmlns:atom="http://www.w3.org/2005/Atom"><atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/docs/2007#spreadsheet" /><atom:title>' + "NewDoc" + "</atom:title></atom:entry>\r\n";
mime_1_string += "\r\n--" + BOUNDARY + "\r\n";
mime_1_string += "Content-Type: text/html\r\n\r\n";
mime_1.setData(mime_1_string, mime_1_string.length);
var hsis = Components.classes[STRINGIS].createInstance(nsIStringInputStream);
var sheader = new String();
sheader += "\r\n";
sheader += "--" + BOUNDARY + '\r\nContent-disposition: form-data;name="addfile"\r\n\r\n1';
sheader += "\r\n--" + BOUNDARY + "\r\n";
sheader += 'Content-disposition: form-data;name="filename";filename="' + "NewDoc" + '"\r\n';
sheader += "Content-Type: application/octet-stream\r\n";
//sheader += "Content-Length: " + data._size + "\r\n\r\n";
hsis.setData(sheader, sheader.length);
var endsis = Components.classes[STRINGIS].createInstance(nsIStringInputStream);
var bs = new String("\r\n--" + BOUNDARY + "--\r\n");
endsis.setData(bs, bs.length);
mis.appendStream(mime_1);
mis.appendStream(buf);
mis.appendStream(endsis);
// console.log(mis);
/* YAHOO.util.Connect.resetDefaultHeaders();
YAHOO.util.Connect.initHeader("Authorization", "GoogleLogin auth=" + this._AUTH);
YAHOO.util.Connect.initHeader("Content-Length", mis.available() - 2);
YAHOO.util.Connect.setDefaultPostHeader("multipart/related; boundary=" + BOUNDARY);
YAHOO.util.Connect.initHeader("Slug", data.name);
YAHOO.util.Connect.initHeader("MIME-version", "1.0");
YAHOO.util.Connect.initHeader("Cookie", "", false);
this._uploadConnection = YAHOO.util.Connect.asyncRequest("POST", "http://docs.google.com/feeds/documents/private/full", {
success: this.uploadSuccess,
failure: this.uploadFail,
scope: this
},
mis); */
// var boundaryString = "-gd-upload---";
// var boundary = "--" + boundaryString;
// var postData = ["", boundary, "Content-Disposition: form-data; name=\"security_token\"", "", aToken,
// boundary, "Content-Disposition: form-data; name=\"formsubmitted\"", "", "true", boundary,
//"Content-Disposition: form-data; name=\"uploadURL\"", "", aURL, boundary + "--"].join("\r\n");
xhr.setRequestHeader("Content-Type","multipart/related; boundary="+BOUNDARY);
//xhr.setRequestHeader("Content-Type","application/msword");
xhr.setRequestHeader("MIME-version", "1.0");
xhr.setRequestHeader("Authorization", "GoogleLogin auth=" + aToken);
// xhr.setRequestHeader("Content-length", postData.length);
xhr.onreadystatechange = function(aEvent) {
if(xhr.readyState == 4 && xhr.status == 200) {
// openDoc(xhr.channel.URI.spec);
}
}
xhr.send(mis);
}
/* function showAlert(title, message) {
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Components.interfaces.nsIPromptService);
promptService.alert(window, title, message);
} */
window.addEventListener("load", function(event) { GoogleDocs.onLoad(event); }, false);
Alles anzeigen
Das Problem: Die Datei z.b. http://www.eltviller.de/skripte/edv/b/script/ikb-98tj.doc wird nicht korrekt hoch geladen, das heißt es wird nur ein Link erstellt (siehe Bild).
http://www.pics-load.net/?v=images/bdSQMIh8bGo.png
Den Code habe ich von der gDocsBar genommen. Ich habe zwar den Fehler gefunden aber das nützt mir nichts. Hier ist der Fehler:
Code
var buf = Components.classes[STRINGIS].createInstance(nsIStringInputStream);
buf.setData(aURL, aURL.length); //<-- hier ist der fehler
Wie muss die postData aussehen damit die Datei korrekt zu Google Docs hochgeladen wird?
Ich habe auch schon auf dieser Seite gesucht habe aber nichts gefunden.
Es wäre nett wenn mir jemand helfen könnte!
PS: Mein Add-on