37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
|
|
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
|
|
|
|
/** ToolsClass */
|
|
class ToolsClass {
|
|
// constructor() {
|
|
// }
|
|
|
|
/**
|
|
* shorturl
|
|
* @param url url
|
|
*/
|
|
shorturl(url) {
|
|
return new Promise((resolve, reject) => {
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.withCredentials = true;
|
|
xhr.onreadystatechange = function () {
|
|
if (xhr.readyState === 4) {
|
|
if (xhr.status >= 200 && xhr.status < 400) {
|
|
var response = JSON.parse(xhr.responseText);
|
|
resolve(response.link);
|
|
}
|
|
}
|
|
};
|
|
xhr.open("POST", "https://api-ssl.bitly.com/v4/shorten");
|
|
xhr.setRequestHeader("Authorization", "Bearer 88188dfe42e71c8da237fd49ffe5979d0fc69bd6");
|
|
xhr.setRequestHeader("Content-Type", "application/json");
|
|
let encodedData = JSON.stringify({
|
|
"long_url": url,
|
|
"domain": "bit.ly"
|
|
});
|
|
xhr.send(encodedData);
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = ToolsClass |