如何使用JavaScript在Pastebin中粘贴文本

How to paste text in Pastebin using JavaScript

本文关键字:文本 Pastebin 何使用 JavaScript      更新时间:2023-09-26

我使用Tampermonkey(与Greasemonkey相同,但用于Chrome)来制作脚本。这个想法是将我写的文本粘贴到Pastebin中。这篇文章是在其他网站上写的。我看到我可以使用GM_xmlhttpRequest,但它不起作用。这是我的代码:

    var charac = new Array(50);
    var i =0
function callkeydownhandler(evnt) {
   var ev = (evnt) ? evnt : event;
   var code=(ev.which) ? ev.which : event.keyCode;
   charac[i]= code;
        i++;
}
if (window.document.addEventListener) {
   window.document.addEventListener("keydown", callkeydownhandler, false);
} else {
   window.document.attachEvent("onkeydown", callkeydownhandler);
}
GM_xmlhttpRequest({
  method: "POST",
  url: "http://pastebin.com/post.php",
  data: "user=mysuser&password=mypassword", //as you can imagine I use my credentials
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  onload: function(response) {
    alert("posted");
    document.getElementById("paste_code").value+=charac[i];
    document.getElementById("submit").click();
  }
});

我确定最后两行不能正常工作,但我不知道为什么。第一个函数工作完美。

我哪里做得不好?我该怎么修理它?

谢谢!div =)

我开发了一个简单的API,可以完全满足您的需求。

包括:Persist BETA

Pastebin有一个API,但它目前不支持编辑帖子。
这就是为什么我需要为pastebin创建两个不同的"服务"…PASTEBIN和PASTEBIN2

如果你不需要编辑,使用PASTEBIN。否则,使用PASTEBIN2。

你需要的第一件事是一个唯一的开发者API密钥。
然后你需要一个用户API密钥。

下面是一些使用我的脚本的例子:

创建新帖子

Persist.write({
    service : "PASTEBIN",
    value   : "...",
    data    : {
        api_dev_key     : "...",
        api_user_key    : "...",
    },
    onload  : function (result) {
        alert("http://pastebin.com/" + result.key);
    }
});

编辑已有文章

Persist.write({
    service : "PASTEBIN2",
    mode    : -1,   // prepend
    key     : "..."
    value   : "...",
    data    : {
        api_dev_key     : "...",
        api_user_key    : "...",
    },
    onload  : function (result) {
        alert("Post #" + result.key + "'nNew value: " + result.value);
    }
});

阅读已有文章

Persist.read({
    service : "PASTEBIN",
    key     : "..."
    data    : {
        api_dev_key     : "...",
        api_user_key    : "...",
    },
    onload  : function (result) {
        alert("'nValue: " + result.value);
    }
});