Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add widget support #797

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Add widget support #797

wants to merge 6 commits into from

Conversation

juuz0
Copy link
Collaborator

@juuz0 juuz0 commented Jul 16, 2022

Widget endpoint: /widget
Fixes #585

@codecov
Copy link

codecov bot commented Jul 16, 2022

Codecov Report

Merging #797 (4cd52b0) into master (8e6d893) will increase coverage by 0.00%.
The diff coverage is n/a.

❗ Current head 4cd52b0 differs from pull request most recent head f42d773. Consider uploading reports for the commit f42d773 to get more accurate results

@@           Coverage Diff           @@
##           master     #797   +/-   ##
=======================================
  Coverage   70.62%   70.63%           
=======================================
  Files          53       53           
  Lines        3707     3708    +1     
  Branches     2060     2061    +1     
=======================================
+ Hits         2618     2619    +1     
  Misses       1087     1087           
  Partials        2        2           
Impacted Files Coverage Δ
src/server/internalServer_catalog_v2.cpp 92.94% <0.00%> (+0.08%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us.

@juuz0
Copy link
Collaborator Author

juuz0 commented Jul 16, 2022

Will fix the code coverage 👍
Obviously not completed but I think we can review it from a user perspective with its current form and if this the way to move forward

@juuz0 juuz0 requested a review from kelson42 July 16, 2022 17:02
@mgautierfr
Copy link
Member

I have simply done a quick review as this is still in draft.

  • We should not have a endpoint in the server to test widget feature. Adding it extend the server with a endpoint served at runtime for the user which is totally useless.
  • Widget feature should not be a runtime feature of js. Server must serve a different content dedicated to widgets when requested on /widget endpoint. (And as we return different content, we don't need a widgettest endpoint to test the behavior when the content is put in a Iframe)

@kelson42
Copy link
Collaborator

kelson42 commented Jul 20, 2022

@juuz0 I don't see any changes folowing @mgautierfr! Any problem? How should I test the feature? Where is the documentation?

@juuz0 juuz0 force-pushed the widgetEndpoint branch 3 times, most recently from d279aff to fb3eaf5 Compare July 28, 2022 21:38
@juuz0
Copy link
Collaborator Author

juuz0 commented Jul 28, 2022

@juuz0 juuz0 marked this pull request as ready for review July 29, 2022 16:08
@kelson42
Copy link
Collaborator

kelson42 commented Aug 1, 2022

@juuz0 Can you please rebase (and resolve the conflicts) now that we have merged the "no jquery" PR?

@juuz0
Copy link
Collaborator Author

juuz0 commented Aug 1, 2022

@kelson42 Done.

@kelson42 kelson42 changed the title add widget support Add widget support Aug 6, 2022
@juuz0
Copy link
Collaborator Author

juuz0 commented Aug 6, 2022

@kelson42 Any updates here? :)

juuz0 added 3 commits August 11, 2022 21:10
Extracts function updateBookCount() from the unnamed function in resize event.
Adds an endpoint /widget to provide kiwix serve widget.
Gives a name to the IIFE wrapping code in index.js - kiwixServe
and exposes updateBookCount through it
Copy link
Collaborator

@kelson42 kelson42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @juuz0 For this first version. It start to be like I expected. Here a few question/remarks:

  • The CSS example does not work for me (i see nothing in red). Are you sure it is OK?
  • We should have a way to filter normaly (via URL) like on library.kiwix.org, it seems to not be the case
  • The ability to show one or many ZIM files based on their name (M/Name) should work as well on welcome page (and should probably be done via a dedicated PR).
  • http://localhost:8181/widget?book=gutenberg_fa_all_2022-01 does not deliver only one book with the M/Name value = gutenberg_fa_all_2022-01

@juuz0
Copy link
Collaborator Author

juuz0 commented Aug 14, 2022

The CSS example does not work for me (i see nothing in red). Are you sure it is OK?

I've checked again, it works on my side with the example in the docs. What browser do you use? I tried on Vivaldi (Chromium based) and Firefox. Exact code of html file?

We should have a way to filter normaly (via URL) like on library.kiwix.org, it seems to not be the case

These work for me again!

http://localhost:8181/widget?book=gutenberg_fa_all_2022-01 does not deliver only one book with the M/Name value = gutenberg_fa_all_2022-01

Yes I confirm this mistake, happened during a recent rebase.

The ability to show one or many ZIM files based on their name (M/Name) should work as well on welcome page (and should probably be done via a dedicated PR).

Looking through the C++ code, I have found there's indeed a way to filer by book name already. Though with two problems:

  1. It doesn't work (http://192.168.18.8:8080/?name=gutenberg_fa_all_2022-04 should give the book but it doesn't, I have found the fix and will be fixing it in the dedicated PR)
  2. It isn't made to accept multiple values which we want here, doing that too.

@kelson42
Copy link
Collaborator

The CSS example does not work for me (i see nothing in red). Are you sure it is OK?

I've checked again, it works on my side with the example in the docs. What browser do you use? I tried on Vivaldi (Chromium based) and Firefox. Exact code of html file?

I test with Firefox, the code comes from the your documentation:

$ cat widget.html 
<!DOCTYPE html>
<html lang="en">
<head>
<title>Widget Test</title>
</head>
<body>
  <iframe src="http://localhost:8181/widget" width=1000 height=1000></iframe>

  <script>
window.onload = function() {
var receiver = document.getElementById('receiver').contentWindow;
function sendMessage() {
    let msg = {
    css: `
    .book__header {
        color:red;
    }`,
    js: `
    function widgetTest() {
    console.log("Testing widget");
    }
    widgetTest();
    `
    }
    receiver.postMessage(msg, 'http://192.168.18.8:8080/widget');
}
sendMessage();
}
</script>

</body>
</html>

and the result looks like

image

I see nothing which is different than without custom CSS. Considering that there is nothing which has the DOM id="receiver", I suspect the documentation to be simply wrong or incomplete.

We should have a way to filter normaly (via URL) like on library.kiwix.org, it seems to not be the case

These work for me again!

Indeed, seems to work. Sorry I can not reproduce the problem.

http://localhost:8181/widget?book=gutenberg_fa_all_2022-01 does not deliver only one book with the M/Name value = gutenberg_fa_all_2022-01

Yes I confirm this mistake, happened during a recent rebase.

The ability to show one or many ZIM files based on their name (M/Name) should work as well on welcome page (and should probably be done via a dedicated PR).

Looking through the C++ code, I have found there's indeed a way to filer by book name already. Though with two problems:

1. It doesn't work (`http://192.168.18.8:8080/?name=gutenberg_fa_all_2022-04` should give the book but it doesn't, I have found the fix and will be fixing it in the dedicated PR)

2. It isn't made to accept multiple values which we want here, doing that too.

All of this seems to be the same problem and actually is 100% independent of the widget system IMO. Can you please open a ticket dedicated to this (and also work on the solution).

An other detail, "Powered by Kiwix" should not be part of the widget IMO. I continue with my testing....

@kelson42
Copy link
Collaborator

Modulo the remarks done earlier, this first version of the new /widget feature looks good to me. Adding @veloman-yunkan for the code review

@juuz0
Copy link
Collaborator Author

juuz0 commented Aug 15, 2022

I see nothing which is different than without custom CSS. Considering that there is nothing which has the DOM id="receiver", I suspect the documentation to be simply wrong or incomplete.

The documentation does have the id=receiver in iframe, only in CSS/JS section :)
There are two mistakes in the code you sent:

  1. id=receiver in iframe
  2. receiver.postMessage(msg, <URL here>); and <iframe src="URL here" width=1000 height=1000></iframe> should have the same URL (I should probably mention it clearly in docs)
    Here's the fixed version:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Widget Test</title>
</head>
<body>
  <iframe id="receiver" src="http://localhost:8181/widget" width=1000 height=1000></iframe>

  <script>
window.onload = function() {
var receiver = document.getElementById('receiver').contentWindow;
function sendMessage() {
    let msg = {
    css: `
    .book__header {
        color:red;
    }`,
    js: `
    function widgetTest() {
    console.log("Testing widget");
    }
    widgetTest();
    `
    }
    receiver.postMessage(msg, 'http://localhost:8181/widget');
}
sendMessage();
}
</script>

</body>
</html>

juuz0 added 2 commits August 15, 2022 18:21
Adds handling for parameters:
disablefilter - disable search filters
disableclick - disable book click action
disabledownload - disable download button
disabledesc - disable description
Added an event listener for message event.
The idea is the website which embeds the widget will send a message using postMessage().
The expected message is:
{
css: // custom CSS code
js: // custom JS code
}
Added documentation for current widget usage, currently supported arguments, using custom CSS/JS
@juuz0
Copy link
Collaborator Author

juuz0 commented Aug 15, 2022

An other detail, "Powered by Kiwix" should not be part of the widget IMO

Done.
Also the commit which provided a way to filter books by name (should be done in C++ side in other PR)

@kelson42
Copy link
Collaborator

kelson42 commented Aug 15, 2022

@juuz0 Please create a dedicated ticket + PR for selecting ZIM files based on there ZIM Name metadata. It should work in general and not only for the widget.

@juuz0
Copy link
Collaborator Author

juuz0 commented Aug 15, 2022

@kelson42 #807

document.querySelector(nodeQuery).innerHTML += optionStr;
const entryList = data.querySelectorAll('entry');
const nodeQueryElem = document.querySelector(nodeQuery);
if (entryList && nodeQueryElem) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When nodeQueryElem == null doesn't the loadAndDisplayOptions() function become a no-op? If so why run fetch(query) at all?

Comment on lines +40 to +41
disableclick (value = N/A)
Disables clicking the book to open it for reading.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me (Firefox 103.0) a blank tab is opened

Comment on lines +1 to +15
const hideNavRule = `
.kiwixNav {
display: none;
}`;
const hideResultsLabelRule = `
.kiwixHomeBody__results {
display: none;
}`;
const hideTagFilterRule = `
.book__tags {
pointer-events: none;
}`;
insertNewCssRules(widgetStyles, [hideNavRule, hideResultsLabelRule, hideTagFilterRule]);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if you introduce custom CSS handling first, then disableSearchFilters(), disableDownload(), disableDescription() and hideFooter() can be rewritten via addCustomCss() in a simpler way.

@kelson42
Copy link
Collaborator

@juuz0 Any feedback?

@stale
Copy link

stale bot commented Nov 2, 2022

This pull request has been automatically marked as stale because it has not had recent activity. It will be now be reviewed manually. Thank you for your contributions.

@stale stale bot added the stale label Nov 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kiwix-serve should propose widgets
4 participants