How can I get the query from the address bar in the Google Custom Search - google-custom-search

I have a search bar within my header and a seperate search page,
When you do a search request in the search bar. It returns a querystring which looks like '?q=querystring', it automatically links this querystring to my search page.
Question
How can I take the querystring from the address bar and use it to fill in the Google Custom Search bar.

You did not specify what you were using, so I will make the assumption that you want the easiest way out:
If you are using the new Element API v2, then you have two options to handle 2-page search:
Full Render
Index.html
<!-- GOOGLE SEARCH JS Search Only Implementation-->
<div>
<script>
(function() {
var cx = 'YOUR_API_KEY';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:searchbox-only resultsUrl="search.html"></gcse:searchbox-only>
</div>
search.html
<!-- GOOGLE SEARCH JS Implementation-->
<div>
<script>
(function() {
//Same code from Index.html
</script>
<gcse:search></gcse:search>
</div>
Custom Index + Search Page Render
You create your own search form at the Index page where it sends a HTTP GET with a parameter that you will hook on the queryParameterName. This will be specified in the gsce element being rendered. An example URL would be http://localhost:3939/search?search_term=Miku
Index.html
<form method="get" action="search.html" name="searchform" id="searchform">
<label for="words">Search:</label>
<input name="search_term" alt="Search_term" value="" size="16" id="words" type="text" accesskey="s">
<button type="submit" value="Submit" accesskey="g">Search</button>
</form>
Search.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<script>
(function() {
var cx = 'YOUR CSE KEY';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:search queryParameterName="search_term"></gcse:search>
</div>
</body>
</html>
From my experience dealing with the element API v2, this is one of the few exposed hooks in which you can customize on the client side. Also, you should render your own code at the CSE Page, then add the queryParameterName.

Related

How can I get the content inside Ace (code editor) after clicking a button?

I have a page with an embedded Ace code editor that will contain Java code. I want to get its contents via a POST request after a button is pressed.
I'm aware that one can do this in order to get the value:
var code = editor.getValue();
However, I'm not too sure how I would get this value from my route handler. Ideally, I'd like to have the editor script inside a separate .js file, but I can't seem to get it to be recognized by anything.
In my handlebars template for /myroute/*
<div class = "bigclass">
some divs
<div id = "containerclass">
<div id = "editor"></div>
<script src = "https://pagecdn.io/lib/ace/1.4.8/ace.js", type = "text/javascript" charset = "utf-8"></script>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/github");
editor.session.setMode("ace/mode/java");
editor.session.setUseWrapMode(true);
editor.setValue("Hello");
editor.clearSelection();
</script>
</div>
some more divs
<div id = "someotherclass">
<form method = "POST" action = "">
<button style = "text-align: center" type="submit" class="btn"> Submit</button>
</form>
</div>
</div>
Route Handler for /myroute/*
app.post('/myroute/*', function(req, res) {
// Get the contents of the editor
});
You've to set an input inside your form, and fill it every time a change is done on the editor.
On clicking submit, you can have the value from the req.body
<div class = "bigclass">
some divs
<div id = "containerclass">
<div id = "editor"></div>
<script src = "https://pagecdn.io/lib/ace/1.4.8/ace.js", type = "text/javascript" charset = "utf-8"></script>
</div>
some more divs
<div id = "someotherclass">
<form method = "POST" action = "">
<textarea name="content" id="content"></textarea>
<button style = "text-align: center" type="submit" class="btn"> Submit</button>
</form>
</div>
</div>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/github");
editor.session.setMode("ace/mode/java");
editor.session.setUseWrapMode(true);
editor.getSession().on("change", function () {
document.getElementById('content').val(editor.getSession().getValue());
});
editor.setValue("Hello");
editor.clearSelection();
</script>
Server:
app.post('/myroute/*', function(req, res) {
const content = req.body.content;
});

Dropzone inside a html form with other form fields not working

I want to add a dropzone inside an existing form but it doesn't seem to work.
When I view the console I get error throw new Error("No URL provided"). When I click upload I get no preview either - all I get is a normal file input.
<link href="../dropzone.css" rel="stylesheet">
<form action="/" enctype="multipart/form-data" method="POST">
<input type="text" id ="Username" name ="Username" />
<div class="dropzone" id="my-dropzone" name="mainFileUploader">
<div class="fallback">
<input name="file" type="file" />
</div>
</div>
<div>
<button type="submit" id="submit"> upload </button>
</div>
</form>
<script src="../jquery.min.js"></script>
<script src="../dropzone.js"></script>
<script>
$("my-dropzone").dropzone({
url: "/file/upload",
paramName: "file"
});
</script>
No url provided error is because $("my-dropzone") is wrong instead it must be $('#mydropzone')
dropzone along with other form, yes this is very much possible, you have to post the data using the URL provided in the dropzone not in the form action. That means all your form data along with the files uploaded shall be posted back to the url provided for the dropzone. A simple untested solution is as below;
<link href="../dropzone.css" rel="stylesheet">
<form action="/" enctype="multipart/form-data" method="POST">
<input type="text" id ="Username" name ="Username" />
<div class="dropzone" id="my-dropzone" name="mainFileUploader">
<div id="previewDiv></div>
<div class="fallback">
<input name="file" type="file" />
</div>
</div>
<div>
<button type="submit" id="submitForm"> upload </button>
</div>
</form>
<script src="../jquery.min.js"></script>
<script src="../dropzone.js"></script>
<script>
$("#mydropzone").dropzone({
url: "/<controller>/action/" ,
autoProcessQueue: false,
uploadMultiple: true, //if you want more than a file to be uploaded
addRemoveLinks:true,
maxFiles: 10,
previewsContainer: '#previewDiv',
init: function () {
var submitButton = document.querySelector("#submitForm");
var wrapperThis = this;
submitButton.addEventListener("click", function () {
wrapperThis.processQueue();
});
this.on("addedfile", function (file) {
// Create the remove button
var removeButton = Dropzone.createElement("<button class="yourclass"> Remove File</button>");
// Listen to the click event
removeButton.addEventListener("click", function (e) {
// Make sure the button click doesn't submit the form:
e.preventDefault();
e.stopPropagation();
// Remove the file preview.
wrapperThis.removeFile(file);
});
file.previewElement.appendChild(removeButton);
});
// Also if you want to post any additional data, you can do it here
this.on('sending', function (data, xhr, formData) {
formData.append("PKId", $("#PKId").val());
});
this.on("maxfilesexceeded", function(file) {
alert('max files exceeded');
// handle max+1 file.
});
}
});
</script>
The script where you initialize dropzone can be inside $document.ready or wrap it as a function and call when you want to initialize it.
Happy coding!!

How to turn off the webcam after using Pubnub?

I started to use Pubnub for making video group chats. However, when I was testing it, I found a little problem: As I connect my computer to their servers, my webcam turns on and never turns off, unless I leave the page.
However, I wish to be able to close a video chatting, and turning off the webcam at the same time. How to do it?
Thank you very much!
EDIT: Here is a code I'm using for my tests, I'm using the one given in the tutorial:
<script src="js/jquery-2.1.4.min.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script src="js/pubnub-3.7.18.min.js"></script>
<script src="js/webrtc.js"></script>
<script src="js/rtc-controller.js"></script>
<div id="vid-box"></div>
<div id="vid-thumb"></div>
<form name="loginForm" id="login" action="#" onsubmit="return login(this);">
<input type="text" name="username" id="username" placeholder="Pick a username!" />
<input type="submit" name="login_submit" value="Log In">
</form>
<form name="callForm" id="call" action="#" onsubmit="return makeCall(this);">
<input type="text" name="number" placeholder="Enter user to dial!" />
<input type="submit" value="Call"/>
</form>
<div id="inCall"> <!-- Buttons for in call features -->
<button id="end" onclick="end()">End</button> <button id="mute" onclick="mute()">Mute</button> <button id="pause" onclick="pause()">Pause</button>
</div>
<script>
var video_out = document.getElementById("vid-box");
var vid_thumb = document.getElementById("vid-thumb");
function login(form) {
var phone = window.phone = PHONE({
number : form.username.value || "Anonymous", // listen on username line else Anonymous
media : { audio : true, video : true },
publish_key : 'pub-c-c66a9681-5497-424d-b613-e44bbbea45a0',
subscribe_key : 'sub-c-35aca7e0-a55e-11e5-802b-02ee2ddab7fe',
});
var ctrl = window.ctrl = CONTROLLER(phone);
ctrl.ready(function(){
form.username.style.background="#55ff5b"; // Turn input green
form.login_submit.hidden="true"; // Hide login button
ctrl.addLocalStream(vid_thumb); // Place local stream in div
}); // Called when ready to receive call
ctrl.receive(function(session){
session.connected(function(session){ video_out.appendChild(session.video); });
session.ended(function(session) { ctrl.getVideoElement(session.number).remove(); });
});
ctrl.videoToggled(function(session, isEnabled){
ctrl.getVideoElement(session.number).toggle(isEnabled); // Hide video is stream paused
});
ctrl.audioToggled(function(session, isEnabled){
ctrl.getVideoElement(session.number).css("opacity",isEnabled ? 1 : 0.75); // 0.75 opacity is audio muted
});
return false; //prevents form from submitting
}
function makeCall(form){
if (!window.ctrl) alert("Login First!");
else ctrl.dial(form.number.value);
return false;
}
function end(){
ctrl.hangup();
}
function mute(){
var audio = ctrl.toggleAudio();
if (!audio) $("#mute").html("Unmute");
else $("#mute").html("Mute");
}
function pause(){
var video = ctrl.toggleVideo();
if (!video) $('#pause').html('Unpause');
else $('#pause').html('Pause');
}
</script>
Note that I tried to find the function through the console in addition to my searches, but I was unable to find it...

Replace content in visual basic web browser

Suppose the following html code is present in web page
<td id="tdwords" colspan="2" class="inputWrap">
<label for="words">Search term</label><input type="text" name="words" value="" id="words" title="Search Crystallography Journals Online" />
</td>
How do I replace
id="tdwords"
with
id="tdset"
when a webpage is opened in my visual basic web browser?
You could use javascript I believe.
Example:
<script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
change_id();
});
function change_id() {
var ids = document.getElementsByClassName('inputwrap'),
i = ids.length;
while(i--) {
ids[i].setAttribute("id", "tdset") ;
}
}
<script>"
Just put this in the Head content and it should happen.

Google share explicit load

Idea:
After dynamic content is loaded, on mouseover I'm trying to render google share button like it says on the official google developer's site.
Code I'm using is:
gapi.plus.render(div);
Facts:
If I change plus to plusone, it renders google plus button instead share. ( Means: Scripts load up )
If I remove {"parsetags": "explicit"}, Buttons load up ( But doesn't load up on hover )
Problem:
Share button doesn't load up.
Debuging links with plus.render and plusone.render:
http://romanlosev.igloro.info/googleshare.php?load=plusone ( plusone - works, but plus+ buttons load up )
http://romanlosev.igloro.info/googleshare.php?load=plus ( plus - doesn't works )
There's a similar question on StackOverflow here.
What you need to do is call the explicit render method for the share buttons. Replacing your function that selects the div with the following code will delay render for the share objects on screen.
$("#clickme").click(function()
{
gapi.plus.go();
});
To render individual buttons, you must pass an object with the action parameter set to share, for example:
gapi.plus.render("plusOne", {action: "share"});
The following is a more complete example, that does asynchronous script loading and renders various share targets, visible here:
<html>
<body>
<p>
<div data-action="share" class="g-plus" id="plusOne"></div>
<button onClick="gapi.plus.render('plusOne', getParamBag('https://www.google.com'))"></button>
</p>
<p>
<div data-action="share" class="g-plus" id="plusTwo"></div>
<button onClick="gapi.plus.render('plusTwo', getParamBag('https://plus.google.com'))"> </button>
</p>
<p>
<div data-action="share" class="g-plus" id="plusThree"></div>
<button onClick="gapi.plus.render('plusThree', getParamBag('https://developers.google.com'))"></button>
</p>
</body>
<script type="text/javascript">
window.___gcfg = {
lang: 'en-US',
parsetags: 'explicit'
};
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
function getParamBag(url){
return {
action: "share",
href: url
};
}
</script>
</html>