How do I make an HTTP request to generate a paste link on PrivateBin?












0















I'm creating a chrome extension that takes some of the HTML from a certain site and converts it into Markdown which is copied to the user's clipboard (to be pasted at https://privatebin.net/).



I have this part down, and I'm now trying to automate the last part and send the Markdown directly to privatebin, generate the paste link, and copy the paste link to the user's clipboard.



I've spent a few hours trying to do this and I've come up with the code below. I've looked at documentation for similar sites like how to do this with pastebin, etc. and tried to fit it to privatebin, but it doesn't work. It does have to be privatebin.



I looked at the API page for privatebin (https://github.com/PrivateBin/PrivateBin/wiki/API) but I'm still not quite sure how to get it to work.



I was shown an example of how it can be done in Kotlin, seen here: (https://cdn.discordapp.com/attachments/524368836703813632/528405075241205763/1546051888.png , https://cdn.discordapp.com/attachments/524368836703813632/528405076147175462/1546051916.png) so I tried doing something similar in JS.



test.onclick = function(){
var url = "https://privatebin.net/"
var req = new XMLHttpRequest();
var params = {
data: "test content",
expire: "never",
formatter: "markdown",
burnafterreading: 0,
opendiscussion: 0
}

req.open('POST', url);
req.setRequestHeader("X-Requested-With", "JSONHttpRequest");
req.send(JSON.stringify(params));

req.onreadystatechange = processRequest;
function processRequest(e) {
if (req.readyState == 4 && req.status == 200) {
alert(req.responseText);
}
}
}


The result is that the alert is empty instead of having a link to the generated url with the data sent. req.responseURL seems to still be the base pastebin site.



Compared to the Kotlin example, I did not do the SJCL stuff with a passphrase and encrypt and I'm missing the User-Agent header, both of which I don't really know how to do.



Also, when I've tried importing SJCL from https://bitwiseshiftleft.github.com/sjcl/sjcl.js in a script tag in popup.html, I get this: https://i.imgur.com/5QHwi0j.png. I've looked for solutions and tried putting



  "content_security_policy": "script-src 'self' https://bitwiseshiftleft.github.com/sjcl/sjcl.js; object-src 'self'",


in manifest.json as well as



<meta http-equiv="Content-Security-Policy" content="default-src *;">


but it still returns the same error.










share|improve this question























  • Using alert isn't proper debugging so it's hard to tell what's wrong without running a demo extension myself. Use devtools debugger where you can set breakpoints and inspect the variables as well as the request itself in the network panel. In content_security_policy don't specify the full script path but just an origin, however you don't need CSP at all since loading external js in a privileged extension context is extremely bad - instead put the script in the package and load it as any other js. BTW why sjcl? There's a built-in Crypto API in browsers.

    – wOxxOm
    Jan 1 at 8:07
















0















I'm creating a chrome extension that takes some of the HTML from a certain site and converts it into Markdown which is copied to the user's clipboard (to be pasted at https://privatebin.net/).



I have this part down, and I'm now trying to automate the last part and send the Markdown directly to privatebin, generate the paste link, and copy the paste link to the user's clipboard.



I've spent a few hours trying to do this and I've come up with the code below. I've looked at documentation for similar sites like how to do this with pastebin, etc. and tried to fit it to privatebin, but it doesn't work. It does have to be privatebin.



I looked at the API page for privatebin (https://github.com/PrivateBin/PrivateBin/wiki/API) but I'm still not quite sure how to get it to work.



I was shown an example of how it can be done in Kotlin, seen here: (https://cdn.discordapp.com/attachments/524368836703813632/528405075241205763/1546051888.png , https://cdn.discordapp.com/attachments/524368836703813632/528405076147175462/1546051916.png) so I tried doing something similar in JS.



test.onclick = function(){
var url = "https://privatebin.net/"
var req = new XMLHttpRequest();
var params = {
data: "test content",
expire: "never",
formatter: "markdown",
burnafterreading: 0,
opendiscussion: 0
}

req.open('POST', url);
req.setRequestHeader("X-Requested-With", "JSONHttpRequest");
req.send(JSON.stringify(params));

req.onreadystatechange = processRequest;
function processRequest(e) {
if (req.readyState == 4 && req.status == 200) {
alert(req.responseText);
}
}
}


The result is that the alert is empty instead of having a link to the generated url with the data sent. req.responseURL seems to still be the base pastebin site.



Compared to the Kotlin example, I did not do the SJCL stuff with a passphrase and encrypt and I'm missing the User-Agent header, both of which I don't really know how to do.



Also, when I've tried importing SJCL from https://bitwiseshiftleft.github.com/sjcl/sjcl.js in a script tag in popup.html, I get this: https://i.imgur.com/5QHwi0j.png. I've looked for solutions and tried putting



  "content_security_policy": "script-src 'self' https://bitwiseshiftleft.github.com/sjcl/sjcl.js; object-src 'self'",


in manifest.json as well as



<meta http-equiv="Content-Security-Policy" content="default-src *;">


but it still returns the same error.










share|improve this question























  • Using alert isn't proper debugging so it's hard to tell what's wrong without running a demo extension myself. Use devtools debugger where you can set breakpoints and inspect the variables as well as the request itself in the network panel. In content_security_policy don't specify the full script path but just an origin, however you don't need CSP at all since loading external js in a privileged extension context is extremely bad - instead put the script in the package and load it as any other js. BTW why sjcl? There's a built-in Crypto API in browsers.

    – wOxxOm
    Jan 1 at 8:07














0












0








0








I'm creating a chrome extension that takes some of the HTML from a certain site and converts it into Markdown which is copied to the user's clipboard (to be pasted at https://privatebin.net/).



I have this part down, and I'm now trying to automate the last part and send the Markdown directly to privatebin, generate the paste link, and copy the paste link to the user's clipboard.



I've spent a few hours trying to do this and I've come up with the code below. I've looked at documentation for similar sites like how to do this with pastebin, etc. and tried to fit it to privatebin, but it doesn't work. It does have to be privatebin.



I looked at the API page for privatebin (https://github.com/PrivateBin/PrivateBin/wiki/API) but I'm still not quite sure how to get it to work.



I was shown an example of how it can be done in Kotlin, seen here: (https://cdn.discordapp.com/attachments/524368836703813632/528405075241205763/1546051888.png , https://cdn.discordapp.com/attachments/524368836703813632/528405076147175462/1546051916.png) so I tried doing something similar in JS.



test.onclick = function(){
var url = "https://privatebin.net/"
var req = new XMLHttpRequest();
var params = {
data: "test content",
expire: "never",
formatter: "markdown",
burnafterreading: 0,
opendiscussion: 0
}

req.open('POST', url);
req.setRequestHeader("X-Requested-With", "JSONHttpRequest");
req.send(JSON.stringify(params));

req.onreadystatechange = processRequest;
function processRequest(e) {
if (req.readyState == 4 && req.status == 200) {
alert(req.responseText);
}
}
}


The result is that the alert is empty instead of having a link to the generated url with the data sent. req.responseURL seems to still be the base pastebin site.



Compared to the Kotlin example, I did not do the SJCL stuff with a passphrase and encrypt and I'm missing the User-Agent header, both of which I don't really know how to do.



Also, when I've tried importing SJCL from https://bitwiseshiftleft.github.com/sjcl/sjcl.js in a script tag in popup.html, I get this: https://i.imgur.com/5QHwi0j.png. I've looked for solutions and tried putting



  "content_security_policy": "script-src 'self' https://bitwiseshiftleft.github.com/sjcl/sjcl.js; object-src 'self'",


in manifest.json as well as



<meta http-equiv="Content-Security-Policy" content="default-src *;">


but it still returns the same error.










share|improve this question














I'm creating a chrome extension that takes some of the HTML from a certain site and converts it into Markdown which is copied to the user's clipboard (to be pasted at https://privatebin.net/).



I have this part down, and I'm now trying to automate the last part and send the Markdown directly to privatebin, generate the paste link, and copy the paste link to the user's clipboard.



I've spent a few hours trying to do this and I've come up with the code below. I've looked at documentation for similar sites like how to do this with pastebin, etc. and tried to fit it to privatebin, but it doesn't work. It does have to be privatebin.



I looked at the API page for privatebin (https://github.com/PrivateBin/PrivateBin/wiki/API) but I'm still not quite sure how to get it to work.



I was shown an example of how it can be done in Kotlin, seen here: (https://cdn.discordapp.com/attachments/524368836703813632/528405075241205763/1546051888.png , https://cdn.discordapp.com/attachments/524368836703813632/528405076147175462/1546051916.png) so I tried doing something similar in JS.



test.onclick = function(){
var url = "https://privatebin.net/"
var req = new XMLHttpRequest();
var params = {
data: "test content",
expire: "never",
formatter: "markdown",
burnafterreading: 0,
opendiscussion: 0
}

req.open('POST', url);
req.setRequestHeader("X-Requested-With", "JSONHttpRequest");
req.send(JSON.stringify(params));

req.onreadystatechange = processRequest;
function processRequest(e) {
if (req.readyState == 4 && req.status == 200) {
alert(req.responseText);
}
}
}


The result is that the alert is empty instead of having a link to the generated url with the data sent. req.responseURL seems to still be the base pastebin site.



Compared to the Kotlin example, I did not do the SJCL stuff with a passphrase and encrypt and I'm missing the User-Agent header, both of which I don't really know how to do.



Also, when I've tried importing SJCL from https://bitwiseshiftleft.github.com/sjcl/sjcl.js in a script tag in popup.html, I get this: https://i.imgur.com/5QHwi0j.png. I've looked for solutions and tried putting



  "content_security_policy": "script-src 'self' https://bitwiseshiftleft.github.com/sjcl/sjcl.js; object-src 'self'",


in manifest.json as well as



<meta http-equiv="Content-Security-Policy" content="default-src *;">


but it still returns the same error.







javascript google-chrome-extension xmlhttprequest






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 7:42









AptoApto

33




33













  • Using alert isn't proper debugging so it's hard to tell what's wrong without running a demo extension myself. Use devtools debugger where you can set breakpoints and inspect the variables as well as the request itself in the network panel. In content_security_policy don't specify the full script path but just an origin, however you don't need CSP at all since loading external js in a privileged extension context is extremely bad - instead put the script in the package and load it as any other js. BTW why sjcl? There's a built-in Crypto API in browsers.

    – wOxxOm
    Jan 1 at 8:07



















  • Using alert isn't proper debugging so it's hard to tell what's wrong without running a demo extension myself. Use devtools debugger where you can set breakpoints and inspect the variables as well as the request itself in the network panel. In content_security_policy don't specify the full script path but just an origin, however you don't need CSP at all since loading external js in a privileged extension context is extremely bad - instead put the script in the package and load it as any other js. BTW why sjcl? There's a built-in Crypto API in browsers.

    – wOxxOm
    Jan 1 at 8:07

















Using alert isn't proper debugging so it's hard to tell what's wrong without running a demo extension myself. Use devtools debugger where you can set breakpoints and inspect the variables as well as the request itself in the network panel. In content_security_policy don't specify the full script path but just an origin, however you don't need CSP at all since loading external js in a privileged extension context is extremely bad - instead put the script in the package and load it as any other js. BTW why sjcl? There's a built-in Crypto API in browsers.

– wOxxOm
Jan 1 at 8:07





Using alert isn't proper debugging so it's hard to tell what's wrong without running a demo extension myself. Use devtools debugger where you can set breakpoints and inspect the variables as well as the request itself in the network panel. In content_security_policy don't specify the full script path but just an origin, however you don't need CSP at all since loading external js in a privileged extension context is extremely bad - instead put the script in the package and load it as any other js. BTW why sjcl? There's a built-in Crypto API in browsers.

– wOxxOm
Jan 1 at 8:07












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993838%2fhow-do-i-make-an-http-request-to-generate-a-paste-link-on-privatebin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993838%2fhow-do-i-make-an-http-request-to-generate-a-paste-link-on-privatebin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory