document.write() and localStorage is not available in chromeApp - javascript

Hi I am creating chroma app using pdftron webviewer.js.I am able to render pdf on browser.but When I am including pdftron webviewer library in my chrome app the I am getting some error. This is web version pdftron webviewer control.
PDFTronWebViewer Sample
I am getting this error:
1)document.write()and localStorage are not available in packaged apps. throw new Error(message);

document.write() and localStorage are not available in Chrome apps.
As simple as that. You'll need to use a library that doesn't rely on those.

Instead of overriding window.localStorage in your index.js file you can make changes to lib/HTML5/ControlUtils.js to change how the _storePreference and _getPreference functions work. Instead of having them use localStorage (which is not available in Chrome apps) you can use Google's storage API https://developer.chrome.com/extensions/storage i.e.
_storePreference: function(key, value) {
chrome.storage.local.set({key: JSON.stringify(value)});
}
If you prefer, you can override these functions in your config.js file i.e.
window.ControlUtils.userPreferences._storePreference = function(key, value) {
chrome.storage.local.set({key: JSON.stringify(value)});
}

Instead of document.write you should use document.createElement

Related

Use ethereumjs-wallet as a browser module

I have a need to create a wallet (generate account address and private key) in the browser without connecting to a node. If I understand correctly, in order to use web3.js we need to set a provider (Metamask or localnode) to use web3.personal.newAccount("SEED", (response) => { console.log(response)})
I found ethereumjs-wallet perfect for my use case but it exists only as a node module. Any way to use it as a browser module or alternatives to that?
Another key management tool from EthereumJS community is the Keythereum
Keythereum is a JavaScript tool to generate, import and export Ethereum keys. This provides a simple way to use the same account locally and in web wallets. It can be used for verifiable cold storage wallets.
A minified, browserified file dist/keythereum.min.js is provided by them for use in the browser. Including this file simply attaches the keythereum object to window:
<script src="dist/keythereum.min.js" type="text/javascript"></script>
You can try with this one.

How do I read a JSON file using HTML?

Here is my code:
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>
$(function() {
var thing = [];
var bar = $.getJSON('C:\Users\cccompro\foo.json', function(obj) {
for (i = 0; i < obj.length; i++) {
thing.push(obj[i]);
}
});
});
</script>
I'm not sure why it doesn't work. "foo.json" contains an array of objects.
If you are trying the code at Question at Chrome or Chromium browsers, launch the browser instance with --allow-file-access-from-files flag set. Note that open instances of Chrome or Chromium should be closed when you launch the browser or the instance will be launched with the open browser instances' configuration folder, instead of with the flag set. You can launch Chrome or Chromium with an existing instance open and honoring the flag by using --user-data-dir flag with value set a different directory than open instance of Chrome or Chromium.
Technically, it is also possible to write to user file system without using an extension with window.webkitRequestFileSystem. Though using chrome.fileSystem within an extension provides an API designed to achieve the read/write.
See
Jquery load() only working in firefox?
Read local XML with JS
How to Write in file (user directory) using JavaScript?
How to use webkitRequestFileSystem at file: protocol
JavaScript/Ajax Write to File
Using <input type="file"> element
How to print all the txt files inside a folder using java script
You cannot read files directly from the users hard drive without the browsers permission. This would be a huge security issue if you could even though there are ways to allow this (checkout guests answer).
You could however try to make the user select the file and then read it with Javascript.
This is called the HTML 5 file API.
However, this doesn't work for any browser and you probably have to use a server anyway in this case.
For more information on this checkout this or this post.

Execute javascript without webview in Android

I'm trying to execute a JS fonction in my Android app.
The function is in a .js file on a website.
I'm not using webview, I want to execute the JS function because it sends the request i want.
In the Console in my browser i just have to do question.vote(0);, how can I do it in my app ?
UPDATE 2018: AndroidJSCore has been superseded by LiquidCore, which is based on V8. Not only does it include the V8 engine, but all of Node.js is available as well.
You can execute JavaScript without a WebView. You can use AndroidJSCore. Here is a quick example how you might do it:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://your_website_here/file.js");
HttpResponse response = client.execute(request);
String js = EntityUtils.toString(response.getEntity());
JSContext context = new JSContext();
context.evaluateScript(js);
context.evaluateScript("question.vote(0);");
However, this most likely won't work outside of a WebView, because I presume you are not only relying on JavaScript, but AJAX, which is not part of pure JavaScript. It requires a browser implementation.
Is there a reason you don't use a hidden WebView and simply inject your code?
// Create a WebView and load a page that includes your JS file
webView.evaluateJavascript("question.vote(0);", null);
For the future reference, there is a library by square for this purpose.
https://github.com/square/duktape-android
This library is a wrapper for Duktape, embeddable JavaScript engine.
You can run javascript without toying with WebView.
Duktape is Ecmascript E5/E5.1 compliant, so basic stuff can be done with this.

Lazy loading knockoutjs with bookmarklet failing

I'm attempting to fix a bookmarklet I wrote to track the URL changes in a single page application, specifically recording timesheet while using asana. It uses a script loader to embed jQuery and KnockoutJS libraries before init. I'm unable to find the ko object in global scope after the KnockoutJS library has initialized and cannot figure out why. To test, login to https://app.asana.com, open the Google Chrome developer tools' Console tab and try the following code:
var koScript=document.createElement('script');
koScript.type='text/javascript';
koScript.src='//ajax.aspnetcdn.com/ajax/knockout/knockout-3.0.0.js';
document.getElementsByTagName('head')[0].appendChild(koScript);
The Network tab shows the script downloading. The Elements tab shows the script as the last child of the head element. Yet ko remains undefined.
The short version is that:
Asana uses an internal module system that uses module.exports and require - the CommonJS standard you may know from node.js.
The knockout.js file checks the environment to determine if it should be setting window.ko or using module.exports or AMD-style define. If it detects CommonJS-style require it sets properties on exports instead of on a global ko object.
Workaround:
You could temporarily "copy" require off first:
_require = require; require = null
And then it should set window.ko as you're expecting!

control JSON is undefined in Windows Phone WebBrowser (IE)

I am currently building an application on Windows Phone 7 using the WebBrowser control. The WebBrowser is navigated to a URL, but I also inject my own javascript into the control.
However the code which seems to work well on other platforms (WebView for Android and iOS) doesn't seem to work in the WebBrowser:
function parseToString(outObject)
{
var outJSON = null;
try{
if(outObject != null){
outJSON = JSON.stringify(outObject);
}
}
catch(err)
{
outJSON = err.message;
}
window.external.Notify(outJSON);
}
I found out that the "control JSON is undefined" is thrown in the try block.
Can someone recommend an alternative method for stringifying the JSON in the WebBrowser/IE? Furthermore, I cannot use external libraries as those javascripts are injected into the WebBrowser.
The WP7 browser does not supply a JSON object, so you will need to use a third party library in order to convert an object into a JSON string. I have used both of these in the past:
Douglas Crockford's JSON library.
The knockoutJS toJSON function.
See this related question:
Serializing to JSON in jQuery
I had the same problem, but then added the following to the top of the HTML document:
<!DOCTYPE html>
This unfortunately doesn't help if you don't have access to the HTML document and are only injecting JavaScript, but someone might find it useful.

Categories

Resources