Here is my popup.js file
chrome.tabs.executeScript( {
code: "window.getSelection().toString();"
}, function(selection) {
document.getElementById("output").innerHTML = selection[0];
});
How do I send selected text along with the url to an external API via POST request?
Following code was able to send POST requests to an external API
popup.js:
chrome.tabs.executeScript( {
code: "window.getSelection().toString();"
}, function(selection) {
//document.getElementById("output").innerHTML = selection[0];
var data = "quote=This%20is%20%dope%20feature%20with%20embed%20option";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "http://localhost:8000/api/excerpt/");
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("authorization", "Token 42f2909aeacc293ac3a33a76485821e6399d5e1472");
xhr.send(data);
});
Related
I am trying to pagination without jquery and in pure javascript.
Pagination javascript working fine in normal, but when I adding ajax and getting from the result, that time not working pagination.
I am taking reference link is
https://coderanch.com/t/597555/frameworks/Countdown-timer-Pagination-Javascript
My ajax script is
function showResult(elem) {
var data_search = elem;
var dataarray = {
"value": data_search
};
var data = JSON.stringify(dataarray);
var request = new XMLHttpRequest();
request.open("POST", "search_ajax.php", true);
request.setRequestHeader("Content-Type", "application/json");
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
var data1 = request.responseText;
document.getElementById('results').innerHTML = data1;
}
}
request.send(data);
}
Kindly suggest a solution, thanks in advance
I've been struggling for hours with following code without success. In my html I have several inputs (type=text, type=date and selects), and a button calling a js function: onclick=SendNewData().
The JS function is something like the following:
function SendNewData() {
var MyData1=document.getElementById("id1").value;
var MyData2=document.getElementById("id2").value;
var MyData3=document.getElementById("id3").value;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open('POST', 'Handler.php', true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status==200) {
document.getElementById("FormNuevaCom").innerHTML = xmlhttp.responseText;
}
}
var data = new FormData;
data.append('DATA1', MyData1);
data.append('DATA2', MyData2);
data.append('DATA3', MyData3);
xhr.send(data);
}
and the Handler.php is something like the following:
if(isset($_POST['DATA1'])) {
$MyVar=$_POST['DATA1'];
echo "Hi there! ".$MyVar." received...";
}
I canĀ“t get any response. Anyone can spot the problem?
I am a newbie in programming.
I have a sensor that is connected to the app via bluethooth. The app sends the data to the cloud service. I got a link from the cloud service that contains the data in a json format. Now I want this data to be displayed on my Website, but its cross domain and whatever I do it says 401 (Unauthorized).
<html>
<head>
<title>Sensor</title>
<script src="jquery-3.2.1.min.js"></script>
</head>
<body>
<h1>Sensor</h1>
<button onclick="myFunctionPost()">Start</button>
<div id="result" style="color:red"></div>
<script>
function myFunctionPost() {
var getJSON = function(url) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status == 200) {
resolve(xhr.response);
} else {
reject(status);
}
};
xhr.send();
});
}; getJSON('https://thetablewheremydatais$format=json').then(function(data) {
alert('Your Json result is: ' + data.result); //you can comment this, i used it to debug
result.innerText = data.result; //display the result in an HTML element
}, function(status) { //error detection....
alert('Something went wrong.');
});
}
</script>
</body>
</html>
Have you tried this line of code before you call the server with xhr.send()
xhr.withCredentials = true;
I want to send email from Chrome extension where user has to enter the email address of the reciepent in the extenion popup window, and the link of the current opened tab will be sent to the entered email address, but I am stuck in the authentication part, it asks for the password of my email as shown in the.
After I enter, it reloads the same page instead of sending the mail. Please help me out where I am getting wrong.
The code is for popup.js page.
document.addEventListener('DOMContentLoaded', function () {
document.querySelector('button').addEventListener('click', getCurrentTabAndUrl);
});
function getCurrentTabAndUrl() {
chrome.tabs.getSelected(null, function(tab) {
var tabId = tab.id;
var tabTitle=tab.title;
var tabUrl = tab.url;
if (tabUrl=="chrome://newtab/") {
document.getElementById("data").innerHTML="Looks like you opened a new tab, please open a web page and click again to Share.";
}else {
document.getElementById("data").innerHTML="subject="+tabTitle+'<br/>'+tabUrl;
var to=document.getElementById("to").value;
sendMessage('me',to,tabTitle,tabUrl);
}
});
}
function sendMessage(userId,to,subject,email) {
authUser();
var base64EncodedEmail = btoa(email);
var request = gapi.client.gmail.users.messages.send({
'userId': userId,
'message': {
'raw': base64EncodedEmail,
"headers":[
{"To":to},
{"Subject":subject}
]
}
});
request.execute();
}
function authUser(){
chrome.identity.getAuthToken(
{'interactive': true},
function(token){
// load Google's javascript client libraries
var url="https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token="+token;
var request = new XMLHttpRequest();
request.onreadystatechange = function(){
if(request.readyState !== 4||request.status !== 200) {
return;
}
var response=JSON.parse(request.responseText);
console.log(response);
};
request.open('POST', url,true);
request.send();
request.setRequestHeader('Authorization','Bearer ' + token);
});
}
After invoking authUser(), which calls an asynchronous API chrome.identity.getAuthToken, you're immediately sending the email so it fails as the token hasn't yet been acquired.
Move that part into a callback which will be executed after getAuthToken completes:
function sendMessage(userId, to, subject, email) {
authUser(function() {
var base64EncodedEmail = btoa(email);
var request = gapi.client.gmail.users.messages.send({
'userId': userId,
'message': {
'raw': base64EncodedEmail,
'headers': [
{'To': to},
{'Subject': subject}
]
}
});
request.execute();
});
}
function authUser(callback) {
chrome.identity.getAuthToken({'interactive': true}, function(token) {
// load Google's javascript client libraries
var url = "https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token=" + token;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState !== 4 || request.status !== 200) {
return;
}
var response = JSON.parse(request.responseText);
console.log(response);
callback();
}
;
request.open('POST', url, true);
request.send();
request.setRequestHeader('Authorization', 'Bearer ' + token);
});
}
I have the following ajax ,on which im trying to post the dataString as a parameter to the php file.
I have tried putting dataString inside xhr.send(dataString);.But it didnt work out.Is there any way around?
dataString = txCryptData;
var xhr = new XMLHttpRequest();
var params="data="+dataString;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
alert(xhr.responseText);
}
}
xhr.open('POST', 'tokenize.php', true);
xhr.send();
In the php I tried $_POST['params']; to fetch the value posted by the ajax req
In PHP use this to get the string sent with ajax :
$data = file_get_contents("php://input");
And in JS :
dataString = txCryptData;
var xhr = new XMLHttpRequest();
var params="data="+dataString;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status==200) {
alert(xhr.responseText);
}
}
xhr.open('POST', 'tokenize.php', true);
xhr.send(params);
$.ajax({
type: 'POST',
// make sure you respect the same origin policy with this url:
url: 'youUrl',
data: {
'Joo': 'bar',
'ca$libri': 'no$libri' // $ sign in the parameter name seems unusual, I would avoid it
},
success: function(msg){
alert('Value' + msg);
}
});
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr.send(params);
You may have to add these lines to your js snippet