This repository has been archived on 2024-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
line-cost-js/ToolsClass.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-12-26 07:15:28 +00:00
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