Metadata display in Neobook like Facebook

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

Locked
User avatar
fkapnist
Posts: 348
Joined: Mon Nov 17, 2014 4:24 pm
Location: Greece
Contact:

Metadata display in Neobook like Facebook

Post by fkapnist »

NeoBook can display the address and title of any page it loads into its WebBrowser Object with simple variables like these:

[WebBrowser1Addr]

[WebBrowser1Title]

But if you want to display page description, keywords and a featured image with that (like Facebook and Twitter do!) here is the JavaScript:

Code: Select all

<script type="text/javascript">
function getMetaData(){
var TheDescription = document.getElementsByName('description')[0].getAttribute('content');
alert(TheDescription);
var TheKeywords = document.getElementsByName('keywords')[0].getAttribute('content');
alert(TheKeywords);
var TheImage = document.querySelectorAll("meta[property='og:image']")[0].content;
alert(TheImage);
// (OR)
var TheImage = document.querySelector("meta[property='og:image']").content;
alert(TheImage);
};
</script>
To execute this code in Neobook and set variables for [TheDescription], [TheKeywords] and [TheImage] , try out these Actions:

[syntax=neobook]BrowserExecScript "WebBrowser1" "window.external.nbSetVar('TheDescription',document.getElementsByName('description')[0].getAttribute('content'));" "JScript"

BrowserExecScript "WebBrowser1" "window.external.nbSetVar('TheKeywords',document.getElementsByName('keywords')[0].getAttribute('content'));" "JScript"

BrowserExecScript "WebBrowser1" "window.external.nbSetVar('TheImage',document.querySelectorAll([#34]meta[property='og:image'][#34])[0].content);" "JScript"

.(OR)
BrowserExecScript "WebBrowser1" "window.external.nbSetVar('TheImage',document.querySelector([#34]meta[property='og:image'][#34]).content;" "JScript"
[/syntax]

I found that getting the featured image is tricky because some sites don't name it "og:image" while other sites don't even have a featured image in their metadata content at all. That can sometimes make the Neobook program search a page for too long until it becomes unstable. If anyone has a workaround for this I would greatly appreciate it....

UPDATE: To use the above Neobook Actions please correct [0] with [#91]0[#93]
Then everything will work just fine.... I went down a blind alley in the examples below but it was fun!


:)

.
Last edited by fkapnist on Sat May 14, 2016 5:31 pm, edited 1 time in total.
User avatar
fkapnist
Posts: 348
Joined: Mon Nov 17, 2014 4:24 pm
Location: Greece
Contact:

Re: Metadata display in Neobook like Facebook

Post by fkapnist »

I must be doing something wrong. The Javascripts work fine from a webpage, but the code in Neobook only returns things like [object] or [IE=EDGE]...
Maybe the Javascript should go into the function library and not be executed directly from WebBrowser? Or perhaps the punctuation marks break the code?

Anyway, I found another way to do the same thing although it is slower and more complicated

[syntax=neobook]
BrowserExport "WebBrowser1" "[thevar]"
StrParse "[thevar]" "</head>" "[varhead]" "[headCount]"
SetVar "[thevar]" "http[varhead1]"
SearchStr "description[#34] content=" "[thevar]" "[thevartitle]" ""
If "[thevartitle]" "=" "0"
SetObjectCaption "TextEntry32" " Meta Description Not Found. "
GotoLine "alldone"
EndIf
StrDel "[thevar]" "1" "[thevartitle]+21" "[thevartitle2]"
SearchStr ">" "[thevartitle2]" "[thevartitle3]" "CaseSensitive"
SubStr "[thevartitle2]" "1" "[thevartitle3]" "[thevartitle4]"
StrReplace "[thevartitle4]" ">" "" "[thevartitle5]" "CaseSensitive"
StrReplace "[thevartitle5]" "[#34]" "" "[thevartitle6]" "CaseSensitive"
StrReplace "[thevartitle6]" "/" "" "[thevartitle7]" "CaseSensitive"
StrReplace "[thevartitle7]" "&nbsp;" " " "[thevartitle8]" "CaseSensitive"
StrReplace "[thevartitle8]" "&amp;" " &" "[thevartitle9]" "CaseSensitive"
StrReplace "[thevartitle9]" "'" "'" "[thevartitle10]" "CaseSensitive"
StrReplace "[thevartitle10]" "itemprop=description" "" "[titleANSItxt]" "CaseSensitive"
FileWrite "[TempDir]ANSItmp" "All" "[titleANSItxt]"
Delay "50"
FileRead "[TempDir]ANSItmp" "All" "[readtitle]"
Delay "50"
If "[readtitle]" "<=" " "
SetObjectCaption "TextEntry32" " Meta Description Undefined. "
GotoLine "alldone"
EndIf
SetObjectCaption "TextEntry32" "[readtitle]"
:alldone
[/syntax]

It copies the page source and writes some files, but it finally displays the Description in TextEntry32. Meanwhile, I found even more short Javascripts that basically do the same thing if they are pasted into a web page. But none of them seem to work In Neobook.

Code: Select all

document.getElementsByTagName('meta').item(property='description');
$("meta[property='description']").attr('content');
$("meta[property='description']")
$('meta[name=description]').attr("content")
document.getElementsByTagName('meta')['description'].content;
document.head.querySelector("[property=description]").content;
document.head.querySelector('meta[name=description]');

I will keep trying to find a simpler solution than what I've got now although it does work...
User avatar
fkapnist
Posts: 348
Joined: Mon Nov 17, 2014 4:24 pm
Location: Greece
Contact:

Re: Metadata display in Neobook like Facebook

Post by fkapnist »

Here is another makeshift method of mine that is lighter and faster than the one above...
I put this text file "GetMetaData.txt" in my Neobook Embedded:

Code: Select all

<script type="text/javascript">
function getMetaData(){
var TheDescription = document.getElementsByName('description')[0].getAttribute('content');
window.external.nbSetVar( '[TheDescription]', TheDescription );
var TheKeywords = document.getElementsByName('keywords')[0].getAttribute('content');
window.external.nbSetVar( '[TheKeywords]', TheKeywords );
var TheImage = document.querySelector("meta[property='og:image']").content;
window.external.nbSetVar( '[TheImage]', TheImage );
};
</script>
</head>
Then I load the current web page into a Browser Object "MetaBrowser" that is small and invisible to the user. The string "</head>" is replaced by GetMetaData.txt. I basically inject javascript into the duplicate source code and call the function needed to return the three variables [TheDescription], [TheKeywords] and [TheImage]. Then I delete the duplicate source code (replace it with about:blank). It's done in a few seconds and I have all my metadata variables.

[syntax=neobook]
BrowserExport "WebBrowser1" "[thevar]"
FileToVar "[Embedded]GetMetaData.txt" "[endhead]"
StrReplace "[thevar]" "</head>" "[endhead]" "[fullsource]" ""
BrowserLoadFromStr "MetaBrowser" "[fullsource]"
Delay "500"
BrowserExecScript "MetaBrowser" "getMetaData();" "JScript"
Delay "500"
SetObjectCaption "TextEntry32" "[TheKeywords]"
BrowserGoTo "MetaBrowser" "about:blank"
[/syntax]

This is smaller in size and faster, and all the variables can be deleted when done, but I still would like to execute the JavaScript straight from the Neobook Action line without having to use external files....

OH! I just spotted this... about:blank is about#058;blank in the Neobook code... maybe the : sign of "og:image" is what broke the code in the first place?
OF COURSE! It is probably the brackets [ 0 ] in the meta javascript that I did not filter for Neobook. Now we know...

:shock:

.
Locked