This repository has been archived on 2023-02-22. You can view files and clone it, but cannot push or open issues or pull requests.
redirect-extention-chrome/popup-func.js

48 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-12-15 21:01:26 +01:00
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;
}
});
}