Speech synthesis keeps speaking on page load - javascript

In one of my PHP project I have integrated speech synthesis. On click of button I am calling a function for text to speech. Like,
var newUtterance = new SpeechSynthesisUtterance();
newUtterance.text = 'Test Message';
window.speechSynthesis.speak(newUtterance);
This works perfect. But when user refreshes the page it keeps speaking where I want to stop it on page load. I tried with,
On document ready function,
if (window.speechSynthesis.speaking) {
window.speechSynthesis.cancel();
}
and also like, On page load function,
var newUtterance = new SpeechSynthesisUtterance();
newUtterance.text = '';
window.speechSynthesis.speak(newUtterance);
But not working. Working on chrome browser. Thanks in advance.

Related

UIWebview Print option

I've created a container iOS app(objective c) which has only one webview. The webview is loaded with a website which is not owned by me. The website that i am accessing from webview has a print button. Tapping on it doesnt do anything. The same website when accessed via mobile browser works fine. I tried with several other websites with print button . It acts the same.
I wrote the following code for airprint. So whenever a native print button is clicked the following code is executed and it works fine.
But how can i execute this code when user clicks on print button on website ???
In other ways is there a way to detect if user clicks the print button on any website ??
UIPrintInfo *pi = [UIPrintInfo printInfo];
pi.outputType = UIPrintInfoOutputGeneral;
pi.jobName = webView.request.URL.absoluteString;
pi.orientation = UIPrintInfoOrientationPortrait;
pi.duplex = UIPrintInfoDuplexLongEdge;
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.printInfo = pi;
pic.showsPageRange = YES;
pic.printFormatter = webView.viewPrintFormatter;
[pic presentAnimated:YES completionHandler:^(UIPrintInteractionController *pic2, BOOL completed, NSError *error) {
// indicate done or error
}];

How do I make a JS script continue to run after navigation (google chrome console)

Say I have a simple script
var i = 0;
test();
function test() {
console.log(i++);
setTimeout(test, 1000);
}
I put it in a Google Chrome console. How do I make it continue to run after the page navigates to another (should continue to print out numbers when browsing the web)?
Maybe save the variable 'i' in onbeforeunload() function, and launch a new script with that saved variable?
How do I make it continue to run after the page navigates to another
you can't, the script cannot continue on another page, it's the browser that runs the javascript in the page, and that will stop it when moving to another page.
(or) should continue to print out numbers when browsing the web?
you have yourself answered this. You can certainly save the counter in localstorage and resume counting on the next page, provided this next page contains the same or similar script and the logic to restore the counter from localStorage.
Or, you can move part of this logic to a server-side script.
I suppose this script is an example and displaying numbers is not really what you want to do.
If you are looking for something to run script even when you have left the browser, I suggest you take a look at Service workers.
If you want more resources, you can check Jake Archibald's blog. He is a chrome developer and he is always talking about service workers. An introduction here.
I didn't see any good suggestions posted already for what I was trying to do but I came up with something that worked for me. I wanted to add a navigation element on the page and not have it go away after navigating. This was on a website that was not managed by me. I removed the innerHtml of the body of the page, added an iframe and pointed it at the page I was on, set it to 100% width and height and removed the border. Then I could navigate within the iframe, but still have my script function run in a set timeout to add the navigation element back to the page after it navigated. Something like this:
document.body.innerHTML = ''
iframe = document.createElement('iframe');
iframe.setAttribute('id', 'iframe');
document.body.appendChild(iframe);
iframe.setAttribute('src', window.location.href);
iframe.style.height = "100%";
iframe.style.width = "100%";
iframe.style.border = "0";
function addContent(){
setTimeout(()=>{
elementToAddTo = iframe.contentWindow.document.getElementById('my-element-id')];
contentToAdd = document.createElement('div');
contentToAdd.innerHTML = `<p>My new content</p>`
elementToAddTo.insertBefore(contentToAdd, elementToAddTo.childNodes[0]);
}, 1000);
}
addContent()
Then in that new content somewhere I had an onchange event which would navigate and call the addContent function by saying window.top.addContent();
onchange="window.location.href = window.location.href.replace(/(param1=.*)/, 'param1='+myNewParamValue); window.top.addContent();">
I Understand this approach makes a lot of assumptions about what you're trying to do and maybe it is only working for me because I'm only changing a param value, but I want to leave this hear in case it helps somebody trying to figure out how to do something similar.

Fire Enter-Keypress event in contenteditable chat div

I'm trying to automatically send a motivational message to a friend who's studying for an exam over hangouts.
I'm using the hangouts app at https://hangouts.google.com/ (using the gmail site wouldn't allow me to access the DOM of the iframe because of the Same-Origin-Policy) and am already able to set the value of the chatbox-div:
var ifrm = document.getElementById('iframeid'); // changes after reload
var doc = ifrm.contentDocument;
var chat = doc.getElementById('chatdivid'); // also changes
chat.innerHTML = "go kathi go!";
This works already.
My problem now is: how do I either send the appropriate keypress to that div or fire the event/callback directly?
I already tried to send an enter-keypress like this:
var enterPress = new KeyboardEvent('keypress', {'key': 'Enter'});
chat.dispatchEvent(enterPress);
But this only deletes the faint informational "Send a message" message and doesn't submit the chat message.
I didn't find solution using JS for your problem, but I think (if you also didn't find it) you can use macroses.
I would use Firefox + iMacros. Chrome has this extension too, but it's worse.

closing browser within chrome page on test complete

So using TestComplete I'm essentially trying to open up a session on chrome, navigate to a web page, and then click a button on that page. after I'm finished I want to close that browser page. I'm having trouble closing the page though. Here is the code I have so far.
function ChromeTest
{
Browsers.Item(btChrome).Run(MyWebAdress);
var browser = Sys.Browser("chrome");
var page = Sys.Browser("chrome").Page(MyWebAdress);
var MyButton = page.ButtonLocation;
MyButton.click();
browser.BrowserWindow.Close(5000);
}
however, at the Close line I get an error that says "Unable to find the object BrowserWindow". Thanks in advance for any help you have.
Change BrowserWindow to BrowserWindow(0) (or whatever index you see in the Object Browser):
browser.BrowserWindow(0).Close(5000);
Or you can call Close() directly on the Chrome process:
browser.Close(5000);

fire fox stuck on transferring data from server

I'm opening new tab by click on button in the home page , and building the new tab from copying some Elements from the home page,on loading event,i do all my work , also I'm changing image src on load event by filling it from the server , i don't know why fire fox stuck on status of "Transferring data from server",can any one help me in this problem?
this is piece of my code:
window.onload = function () {
document.getElementById('demo_tab_info').innerHTML=opener.document.getElementById('demo_tab_ifo').innerHTML;
if(document.getElementById("Have_Patient__Photo_image" ).value =='true')
{
document.getElementById("pat_<%=Pat_Acct%>_pic_area").innerHTML="<img name='pat_<%=Pat_Acct%>_pic_image' id='pat_<%=Pat_Acct%>_pic_image' src='' onload=\"Fix_Dimensions('pat_<%=Pat_Acct%>_pic')\"/>";
var Image_Element_Name = 'pat_<%=Pat_Acct%>_pic_image';
var Image = document.getElementById(Image_Element_Name);
Image.src='/jspapps/apps/file_uploader/view_image.jsp?table=pat_dri_images&db=admin&im_type=pat_pic&pat_acc=<%=Pat_Acct%>&ts='+(new Date()).getTime();
}
}
Install Firebug and use the "Net" tab to see which connections are made and which one is kept open.

Categories