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

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