BrowserCouch, help with basic example - javascript

I'm trying to get a basic example of BrowserCouch working... the one shown here: http://hg.toolness.com/browser-couch/raw-file/blog-post/tutorial.html
My code:
<script type="text/javascript" src="browser-couch.js"></script>
<script type="text/javascript">
BrowserCouch.get('blog-posts',
function onRetrieveCb(db) {
blogDb = db; /* Save the DB for later. */
});
</script>
I get this error in the Chrome and FF console:
BrowserCouch is not defined
I've double checked and the browser-couch.js file is correctly src'd.
How can I get this to work?

Looks like you're using code from the blog post but using a newer version of the JavaScript and the newer one has a different API. From the github page:
var database = BrowserCouch('foo');
database.onload(function(){
database.get('bar', function(d){console.log(d)});
});
But the older version wanted what you're doing:
BrowserCouch.get('blog-posts',
function onRetrieveCb(db) {
blogDb = db; /* Save the DB for later. */
}
);
So I think you need to match your API usage to the library version.

Related

"Cookies not defined" in standalone js-cookie javascript lib?

I am not a javascript guru, but try to use js-cookie.
I included the script: https://github.com/js-cookie/js-cookie: I downloaded it (LINK), and put it in my own js file on the server.
I then include it in a test file and read some cookie, but it keeps showing me the error "Cookies is not defined" in the browser console. What am I doing wrong :( ?
Code:
<html><head>
<script type="javascript" src="https://server/cookies.js"></script>
<script>
console.log("ALL COOKIES: " + Cookies.get());
</script></head>
<body></body>
I've not used the library, but a quick look at the source code shows that it exports Cookies with an uppercase C.
if (!registeredInModuleLoader) {
var OldCookies = window.Cookies;
var api = window.Cookies = factory();
api.noConflict = function () {
window.Cookies = OldCookies;
return api;
};
}
So try using the correct case.
console.log("ALL COOKIES: " + window.Cookies.get());
Also, everything on window is global. So you can simplify the code to this.
console.log("ALL COOKIES: " + Cookies.get());
Next time, in the JavaScript console on the browser. Just type window and enter to see what variables are global. You can also call it directly in the console to see what happens Cookies should print out a JavaScript object with descriptions of what functions it has.
If it's undefined then it wasn't loaded or is not global.
UPDATED:
The browser is not loading the JavaScript library because the mime-type is wrong. You have to use application/javascript as here:
<script type="application/javascript" src="https://server/cookies.js"></script>
You are using wrong versions of cookie.js on different routes/pages
Use the Latest
<script src="https://cdn.jsdelivr.net/npm/js-cookie#2/src/js.cookie.min.js"></script>
If it is not working, then try
<script src="https://cdn.jsdelivr.net/npm/js-cookie#rc/dist/js.cookie.min.js"></script>
Quick fix : Just use window. before calling it
window.Cookies.get()
Inspired by #ucMedia's answer, you can add the following line at the beginning of a script to fix any issues.
var Cookies = window.Cookies;

Oracle MAF : TypeError: Object[object Object] has no method 'signature'

I am developing the mobile application using Oracle MAF. In that application I need to implement the Signature capture functionality.
For that I have downloaded the Signature Capture Demo app from this link.
And trying to integrate with my application. I did all the necessary settings mentioned in that link.
But after running the application I am getting following error
TypeError: Object[object Object] has no method 'signature'
The Piece of code as follows
<![CDATA[
<script type="text/javascript">
(function()
{
makeSig = function() {
try
{
var sigElement = document.getElementById("sig");
if (sigElement == null)
alert("sigElement not found");
var sigJq = $(sigElement);
sigJq.signature();
sigJq.signature({guideline: true});
}
catch (problem)
{
alert("Problem with verbatim code: " + problem);
}
}
window.setTimeout(makeSig, 250);
})();
</script>
<div id="sig" style="height:200px;width:99%"></div>
]]>
Please let me know why it is not able to find the signature method. I have also included all the required .js files in the project.
Thanks in Advance.
Make sure you are properly including the library:
<script type="text/javascript" src="js/jquery.signature.js"></script>
then try something like this
var sigJq = $("sig");
sigJq.signature();

Issue on log4js File Appender

I am using Log4js Library to print the log message in a file. In that I am getting 2 issues. Such as:
12:00:40 PM ERROR Log4js - TypeError: netscape.security.PrivilegeManager is undefined
in Non-IE Browser. Please tell me how to fix this exception. While Using IE , It's
working fine.
How to get the CATALINA_HOME in the JavaScript part. I am using Tomcat.
To get this CATALINA_HOME in Log4j, we need to write as follow
log4j.appender.FILE.File=${catalina.base}/logs/MyLogs.log.
If I'll write the catalina.base, not working in the JavaScript.
Here is my code please have a look on my code and detect the error causing line to have the solution:
<script type="text/javascript">
function myFunction(name) {
var date = new Date();
var log = Log4js.getLogger("fileAppender");
log.setLevel(Log4js.Level.ALL);
var toAppend=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
var fileAppender = new Log4js.FileAppender("${catalina.base}\\logs\\JSLogging."+toAppend+".log");
log.addAppender(fileAppender);
log.debug("My Debugging");
log.info("My Information");
}
</script>
try adding <script type="text/javascript" src="log4javascript.js"></script> in the code seems it is not able to get the reference of the main script.
see if this can help you
<script type="text/javascript">
// Create the logger
var log = log4javascript.getLogger();
// Create a PopUpAppender with default options
var popUpAppender = new log4javascript.PopUpAppender();
// Change the desired configuration options
popUpAppender.setFocusPopUp(true);
popUpAppender.setNewestMessageAtTop(true);
// Add the appender to the logger
log.addAppender(popUpAppender);
// Test the logger
log.debug("Hello world!");
</script>
enter code here
use this link

Use Javascript to get the list of a user's Github repositories

First of all, thanks for reading.
I am hosting my current projects on GitHub. Using GitHub Pages, I ]host my personal blog, you can reach the blog here.
On the blog, I have a page dedicated to all the projects I am currently working on. Basically, I wanted to display the list of all my on-going projects automatically, via querying GitHub.
While Googling a lot, I found that this can be achieved using JavaScript.
I tried it, but it didn't work as expected. When loading the page, I just get the text message 'Querying GitHub for repositories'. And nothing seems to happen.
I contacted GitHub maintainers, and they kindly replied that this technique uses an outdated version of the GitHub API.
As I am not experienced in JavaScript, can anyone help me to fix it ?
Regards,
Roland.
Here is the code I used inside the HTML page
<div id="opensource-projects"></div>
<!-- JavaScript to load and display repos from GitHub -->
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="/javascripts/git.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#opensource-projects").loadRepositories("Yonaba");
});
</script>
Then, inside the file git.js, I have the following:
// http://aboutcode.net/2010/11/11/list-github-projects-using-javascript.html
jQuery.githubUser = function(username, callback) {
jQuery.getJSON("http://github.com/api/v1/json/" + username + "?callback=?", callback);
}
jQuery.fn.loadRepositories = function(username) {
this.html("<span>Querying GitHub for " + username +"'s repositories...</span>");
var target = this;
$.githubUser(username, function(data) {
var repos = data.user.repositories;
sortByNumberOfWatchers(repos);
var list = $('<dl/>');
target.empty().append(list);
$(repos).each(function() {
list.append('<dt>' + this.name + '</dt>');
list.append('<dd>' + this.description + '</dd>');
});
});
function sortByNumberOfWatchers(repos) {
repos.sort(function(a,b) {
return b.watchers - a.watchers;
});
}
};
#jcolebrand: Thanks for your kind help, but i didn't really get what you meant. I tried sending some command to Chrome's console, too. I guess $ is an alias for jQuery, isn't it? Well, sending same stuff The console just outputs multiple objects, describing my repos. Awesome!
I think the issue is now parsing them properly and display them. Gee, I need to learn JavaScipt syntax for that...
The script that you posted isn't working because the URL is for the older API. Change the URL to this and it should work for you.
https://api.github.com/users/YOUR_USERNAME_HERE/repos?callback=CALLBACK
Note: callback=<YOUR_CALLBACK> is optional.
http://developer.github.com/v3/ is pretty explicit on how to do this. In fact, since my username there and here are the same, let me show you.
I opened my repo page on github, https://github.com/jcolebrand (so this is evident so far) and pressed F12 in Chrome.
I interrogated to see that jQuery is indeed installed, because I like shortcuts when I'm doing examples.
I then tested $.getJSON('//api.github.com/users/jcolebrand/repos',{},function(data){console.log(data)}) exactly from the API page, as it says, and lo and behold, I was granted the five repos I see for myself.
Here are the things I have not done: I did not acquire an API key, I did not work via API, and I used my existing credentials. Keep those things in mind, but that's how to improve it going forward.
you can use a github API lib too. This lib is my favourite https://github.com/michael/github
Extending to #JColebrand's answer with the JQuery shortcut to XMLHttpRequest, $.getJson() , here is an API Call that works in 2020.
user = 'tik9'
apirepo = `https://api.github.com/users/${user}`
listrepos = document.createElement('ul')
document.getElementById('github').appendChild(listrepos)
$.getJSON(apirepo + '/repos', function (data) {
console.log('data now', data)
function compare(a, b) {
if (a.watchers > b.watchers) {
return -1
}
if (a.watchers < b.watchers) {
return 1
}
return 0
}
data.sort(compare)
data.forEach(v => {
listItemRepo = document.createElement('li')
listrepos.appendChild(listItemRepo)
hlink = document.createElement('a')
listItemRepo.appendChild(hlink)
hlink.textContent = `${v.description} | Stars: ${v.watchers}`
hlink.href = v.html_url
})
})
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
</head>
<h4><span id=repocount></span> Github Repositories</h4>
<div id=github></div>

Include remoted Library (API 4.5 Mapy.cz) to Google Chrome Extension

I am confused. I'am trying to make little chrome extension (popup) and I need connect to remoted API.
This is, what I would use, if it is a standard web page:
<script type="text/javascript" src="http://api4.mapy.cz/loader.js"></script>
<script type="text/javascript">Loader.load();</script>
I experimented with including new element script to head (not works for me). But I couldn't believe, there is no easier way...
Please, show me the best way.
EDIT:
Linking the API loader is fine and works. Thanks to #serg. So, my code of popup looks like this:
<script type="text/javascript" src="http://api4.mapy.cz/loader.js"></script>
<script type="text/javascript">
Loader.load();
var center = SMap.Coords.fromWGS84(16.61574, 49.20315);
</script>
Object Loader is defined and it is OK. Loader should load the whole API either object SMap. But SMap is undefined. What next?
If you need to make ajax requests to this remote API, you need to list API domain in the permissions in your manifest:
{
"permissions": [
"http://api4.mapy.cz/"
],
}
Finally I ask developer from Mapy.cz and they gave the solution. Instead of using Loader.load() which cause including additional script and async load - use this way:
Loader.async = true;
Loader.load(null, null, function(){
alert(123); // it works...
// custom code calling objects etc. from API
// ...
});
Final Example of usage

Categories