48 lines
1.6 KiB
JavaScript
48 lines
1.6 KiB
JavaScript
|
const searchButton = document.getElementById('searchButton');
|
||
|
if (searchButton) {
|
||
|
searchButton.addEventListener('click', () => {
|
||
|
searchRedirect('test');
|
||
|
})
|
||
|
}
|
||
|
const newButton = document.getElementById('newButton');
|
||
|
if (newButton) {
|
||
|
newButton.addEventListener('click', () => {
|
||
|
newRedirect('test');
|
||
|
})
|
||
|
}
|
||
|
|
||
|
function searchRedirect(host) {
|
||
|
const request = new XMLHttpRequest();
|
||
|
const url = 'https://url.example.com/rest/v3/short-urls';
|
||
|
request.open('GET', url);
|
||
|
request.setRequestHeader('X-Api-Key', 'apikeyhere');
|
||
|
request.send();
|
||
|
request.onload = (e) => {
|
||
|
var shortUrls = request.response
|
||
|
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
|
||
|
let url = tabs[0].url;
|
||
|
alert(shortUrls)
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function newRedirect(host) {
|
||
|
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
|
||
|
let currentUrl = tabs[0].url;
|
||
|
const url = 'https://url.example.com/rest/v3/short-urls';
|
||
|
let sendData = {
|
||
|
"longUrl": currentUrl,
|
||
|
"tags": ["Added by BOTv1"]
|
||
|
};
|
||
|
const request = new XMLHttpRequest();
|
||
|
request.open('POST', url);
|
||
|
request.setRequestHeader('accept', 'application/json');
|
||
|
request.setRequestHeader('X-Api-Key', 'apikeyhere');
|
||
|
request.setRequestHeader('Content-type', 'application/json');
|
||
|
request.send(JSON.stringify(sendData));
|
||
|
request.onload = (e) => {
|
||
|
var responseData = request.response;
|
||
|
document.getElementById("p1link").innerHTML = responseData;
|
||
|
}
|
||
|
});
|
||
|
}
|