Android - Click on webpage link with Javascript - javascript

I'm developing an app which has to:
Open a webpage in a Webview;
Check the available links and click on the link which contains a certain word (I.e. http://www.domain.com/myword); after the click, the link will open in the same webview.
I tried the following code but it' giving this error "E/Web Console(6169): Uncaught SyntaxError: Unexpected identifier at null:1".
WebSettings myBrowserSettings = myBrowser.getSettings();
myBrowserSettings.setJavaScriptEnabled(true);
Log.d("Stefano", "JS enabled");
myBrowser.loadUrl("javascript:document.getElementsByTagName('a[href*='myWord']')[0].click();");
Can you help me?
Consider that I'm using document.getElementsById('My ID') in another part of the app (to click a button) and it's working correctly; but here, i'm not able to get getElementsByTagName working.
Thanks for your time and availability
UPDATE:
I replaced 'a[href*='myWord']' with \"a[href*='myWord']\" as suggested by the user but now i have this error E/Web Console(2683): Uncaught TypeError: Cannot call method 'click' of undefined at null:1.
Any idea?

'a[href*='myWord']'
This reads as string 'a[href*=', var myWord, string ']'
Presumably, you are trying to do this:
\"a[href*='myWord']\"

Related

Playing audio file returns "Uncaught (in promise)" but works in console

I'm trying to play audio files (I've tried many). All of them are mp3s.
I've tested the following on both MAMP localhost and also by just running it in the browser.
I use the following javascript:
var testSound = new Audio();
testSound.src = "a.mp3"
setTimeout(testSound.play.bind(testSound),100)
This returns the error:
Uncaught (in promise)
Trying to catch it:
var testSound = new Audio();
testSound.src = "a.mp3"
setTimeout(playSound,100)
function playSound () {
testSound.play().then(response => {
}).catch(e => {
console.log(e);
})
}
returns nothing ("")
But if I now turn to the console and simply type:
testSound.play()
The sound plays as it's supposed to.
Even if I comment the third line of the first code snippet like:
//setTimeout(testSound.play.bind(testSound),100)
Edit:
Even if people don't know what the solution is I'd still be interested to know if they can reproduce the error.
Edit 2:
By the way, the problem doesn't persist in Firefox or Safari.
If you read the full error message associated with the exception, you'll get a better explanation:
❌ Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. https://goo.gl/xX8pDD
Google's article "Autoplay Policy Changes" (linked in the message above) explains the situation in detail.
The short version is: if you want to play audio or video, you need to wait to do it until the user has clicked something on the page.

Chrome webview tag call getZoom function but receive "is not a function"?

I'm currently writing a Chrome App that uses a webview tag.
I want to get the current zoom factor of the webview element, so I write down this:
var zoom = 1.0;
var webview = document.querySelector('webview');
webview.getZoom(function(zoomFactor){zoom = zoomFactor;});
When I reload the app, I get this error: Uncaught TypeError: webview.getZoom is not a function
However, when I write the third line in console, I get the right value of zoom.
Here is the link to the webview docs.
Please help me.

Using native images in Cordova

I want to use custom images for tab bar icons, as set in this line using this extension: https://github.com/squerb/cordova-ios-tab-bar (imageName is passed through the extension from the javascript):
item = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed: imageName] tag:tag];
I'm struggling to work out where I need to put the image in my Cordova project so that it's accessible by the native code. If indeed that's possible.
I've tried this:
item = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:#"www/assets/failed.png"] tag:tag];
But that results in this error:
2016-02-01 15:52:02.894 MovidiamWeb[2527:1070985] -[NSNull length]: unrecognized selector sent to instance 0x1a1e33ea8
2016-02-01 15:52:02.897 MovidiamWeb[2527:1070985] *** WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: <NSInvalidArgumentException> -[NSNull length]: unrecognized selector sent to instance 0x1a1e33ea8
The second form, referencing a path that is visible in xcode project directory, in cordova's case, 'www/images/whatever.png' should work fine, just had to give it a few goes before it started working for some reason.

selenium javascript - how to refer to the test runner window?

In my app, in the [firefox] browser javascript console I can do this:
elems=document.getElementsByTagName("a")[3]; elems.click();
and the 3rd anchor is clicked
How can I do that using the SeleniumIDE ?
I have a Test Case which does:
store
javascript{elems=document.getElementsByTagName("a")[3]; elems.click();}
dummy
but I just get the somewhat standard invalid javascript error of [error] Unexpected Exception: fileName -> chrome://selenium-ide/content/selenium-core/scripts/selenium-api.js, lineNumber -> 2535, columnNumber -> 0
I have also tried
javascript{elems=driver.document.getElementsByTagName("a")[3]; elems.click();}
and
javascript{elems=driver.getElementsByTagName("a")[3]; elems.click();}
but neither worked.
Have you tried this?
selenium.getEval("var window = this.browserbot.getUserWindow(); var elems = window.document.getElementsByTagName('a')[3]; elems.click();");

TwitterWidget within SilverStripe

I am trying to integrate TwitterWidget within SilverStripe.
That's the plugin I am trying to use:
https://twitter.com/about/resources/widgets/widget_profile
If I put this code in a simple *.html file the widget works. Unfortunately I get an error if I try to use it in a *.ss file.
I downloaded the source code to find out where the error occurs. After several hours I found something pretty confusing:
That's the piece of code where the error occurs:
document.write('<div class="twtr-widget" id="'+this.id+'"></div>')
FireBug:
uncaught exception: [Exception... "An attempt was made to use an object that is not, or is no longer, usable" code: "11" nsresult: "0x8053000b (NS_ERROR_DOM_INVALID_STATE_ERR)" location: "http://www.domain.com/mysite/javascript/widget.js?m=1321187964 Line: 257"]
LeftAn...8532904 (Zeile 883)
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
After a few refreshes sometimes FireBug reports:
An attempt was made to use an object that is not, or is no longer, usable
[Bei diesem Fehler anhalten] document.write('<div class="twtr-widget" id="'+this.id+'"></div>')
Google Chrome JavaScript Debugger:
Uncaught TypeError: Object #<Document> has no method 'write' //widget.js:257
TWTR.Widget.init //widget.js:257
TWTR.Widget //widget.js:211
(anonymous function) //?stage=Stage&flush=1:117
f.extend._Deferred.e.resolveWith //jquery-1.6.4.min.js:2
e.extend.ready //jquery-1.6.4.min.js:2
c.addEventListener.C //jquery-1.6.4.min.js:2
This is a little incomplete to debug, but why not use one of the readily available widgets? Like http://www.silverstripe.org/twitter-widget-pack-widget/

Categories