InAppBrowser inject script (using executeScript) - ios

InAppBrowser js scripts injection using {code: 'some code'} param is working perfectly but not with {file: 'local file url'} param.
var ref = window.open('http://apache.org', '_blank', 'location=yes');
ref.addEventListener('loadstop', function() {
ref.executeSript({file: "myscript.js"});
});
how do I go about injecting script using the file param to inject my local js script?
does it require absolute file path or relative?
Must file be hosted on the child website?
It seem like a mysterious complicated thing to do as I have a few lines of script and can't embed it all using ref.executeSript({codedetails, callback: "myscript.js"});

I had the same problem. Using cordova3.1.0.
ref.executeScript(
{
file: "http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"
//webURL works fine with 'file'
}, function()
{ 
$.get("js/myscript.js", function(data)
{ //workaround to obtain code using jQuery.get
ref.executeScript(
{
code: data
}, function()
{ 
console.log('ref.executeScript done');
});
});
});
When I set the file URL as webURL http://.... ,
it worked, but I could not figure out how to point the local js file,
so I obtain code strings using $.get "js/myscript.js".
The sample code illustrates: jQuery is already installed on the phonegap App, and also trying to use jQuery on the target inAppBrowser app. Just in case.

Related

Using rails asset in javascript

I'm using rails to build a site, and I want to embed a swagger doc in one of the pages. The swagger yaml file is stored in /app/assets/myfile.yaml. In the swagger embed code (javascript) I've tried a variety of approaches:
// in myswagger.html.erb
window.onload = function() {
const ui = SwaggerUIBundle({
url: "<%= asset_path('myfile.yaml') %>",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
]
})
}
I've also tried a bare path to /app/assets/myfile.yaml, document_path('myfile.yaml'), etc. But every time we end up with Fetch error Not Found /swagger.yaml.
What's the proper way to access this file and get it embedded in the javascript?
fast answer:
put the file yaml in your public folder
otherwise check what is generated at the browser level, by inspecting the page

externally_connectable and Firefox WebExtensions

I am trying to convert a Chrome Extension to Firefox using the new API WebExtension.
Everything works fine except the use of chrome.runtime.sendMessage() in a webpage. The goal is to communicate with the addon and pass some data.
For that, I am using the property "externally_connectable" as written here : can-a-site-invoke-a-browser-extension
background.js
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
sendResponse({
success: true,
message: 'ok'
});
return true; // Bug chrome, close channel otherwise
});
In webpage
chrome.runtime.sendMessage(EXTENSION_ID, {type: 'show', data: 'test'}, function(response) {
if (response.success && !response.success) {
console.log(response.message);
}
});
In chrome, the communication works fine but in Firefox, the code executed in the webpage doesn't work : "chrome is not defined".
Is there another var to use instead of "chrome" or is it not implemented ?
I've found nothing about this on the web :( Thanks
Web extensions do not support externally_connectable website scripts but you can communicate between website scripts and extension scripts as shown in this example
https://github.com/mdn/webextensions-examples/tree/master/page-to-extension-messaging
Try use WebExtension APIs with "browser" namespace
browser.runtime.sendMessage(...)
All available APIs on mozilla development
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Content_scripts#WebExtension_APIs

Parsing a XML File in Jquery

I had an html file "search.html" and an xml file "videos.xml" located in same directory, I used the following code to parse it:
$.ajax({
type: "GET",
url: "videos.xml",
dataType: "xml",
success: parseXml
});
function parseXml(xml) {
$(xml).find("Video").each(function () {
var id = $(this).find("id").text();
alert(id);
}
}
This code works in the server(when i run as a web application)
i.e localhost:8080/mysite/search.html
but it is not working locally without server.
i.e file:///D:/mysite/search.html
I think it could not get the path of xml file, i tried by giving both relative path and absolute path but it does not work. So please help me to solve this.
thank you,
P.Naresh

autocomplete showing self.element.propAttr error

I am using Jquery ui Autocomplete.But it show error autocomplete showing self.element.propAttr error.
this is my ajax code
$.ajax({
url: "persons.xml",
dataType: "xml",
success: function( xmlResponse ) {
var data = $( "person", xmlResponse ).map(function() {
return {
value: $( "name", this ).text()
};
}).get();
$( "#birds" ).autocomplete({
source: data,
minLength: 0
});
}
});
I am using xml for response but that doesnot seem to be the problem it seems some function in javascript is deprecated.
Can anyone give me any solutions for this?
Add this lines in front of your statement:
jQuery.fn.extend({
propAttr: $.fn.prop || $.fn.attr
});
I was facing this problem when refactoring my javascript and found that the problem was I removed jquery.ui.core.js, and instead was using only jquery-ui-1.9.1.custom.min.js.
I created this file using the Download Builder at the Jquery UI website with everything checked. Correct me If I am wrong but jquery-ui-1.9.1.custom.min.js should have contained all the javascript necessary to run all the jquery ui addins (in this case autocomplete was failing).
Adding the reference back to jquery.ui.core.js fixed the bug.

Broken relative Url in jQuery getJSON Ajax Method

The Url for my development environment is:
http://localhost/mysite/blah...
I am using jQuery & getJSON to perform some ajax actions on my site, which work fine all the time I specify the url as:
/mysite/controller/action
..but this is not ideal as I don't want to hardcode my development url into my seperate jQuery include files.
When the site goes live, it'll be fine to have controller/action or /controller/action as the url as that will resolve ok, but for the development site, it's no go.
I've tried:
controller/action
..but this returns a 404, which suprised me as I thought the lack of / at the front of the url would prevent from looking at the website root.
There must be a neat solution to this?
I would do this by inserting a global constant in my HTML header:
<script type="text/javascript">
var BASE_URL = '/mysite/';
</script>
That would be inserted from your server so it can be dynamically changed.
Later in your script, you'll be able to make AJAX requests with (jQuery style here):
$.ajax( BASE_URL + '/controller/action', ...);
If you're in
/mysite/controller/action
then the correct relative path to
/mysite/some_other_controller/some_other_action
is
../../some_other_controller/some_other/action
You can use this code to get the current path of the .js script and use it for calculate your relative path.
var url;
$("script").each(function () {
if ($(this).attr("src").indexOf("[YOUR_SCRIPT_NAME.JS]") > 0) {
url = $(this).attr("src");
url = url.substr(0, url.lastIndexOf("/"));
return false;
}
});
var final_url = url + "/your_target_script.js"
Replace YOUR_SCRIPT_NAME with the unique name of your script.

Resources