trevorcampbell / website_diff

MIT License
2 stars 1 forks source link

Add check for jQuery and insert? #15

Closed trevorcampbell closed 3 weeks ago

trevorcampbell commented 1 month ago

I noticed here:

https://github.com/trevorcampbell/website_diff/blob/fe6710ae6c0cd079bbbf444c8c4bb7754b6b7afb/website_diff/page.py#L79

we just insert the js and css for website_diff. But the website_diff javascript uses jQuery, so that has to be loaded too doesn't it?

We should test if this fails for websites that don't use jQuery, and if it does, add a check to see if the website already loads jQuery and if not load it.

trevorcampbell commented 1 month ago

I think the way to do it is to include a line here https://github.com/trevorcampbell/website_diff/blob/fe6710ae6c0cd079bbbf444c8c4bb7754b6b7afb/website_diff/page.py#L79

that loads jquery dynamically based on whether it was already loaded:

    jq_soup = BeautifulSoup('<script src="dynamic_load_jquery.js"></script>', 'html.parser')
    soup.select_one("head").append(jq_soup)

And then in dynamic_load_jquery.js file in the static/ dir,

if (typeof jQuery == 'undefined') {  
  document.write('<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>');
}

And then make sure we copy in the file in the CLI along with the other js files https://github.com/trevorcampbell/website_diff/blob/fe6710ae6c0cd079bbbf444c8c4bb7754b6b7afb/website_diff/cli.py#L18