arekolek / DuolingoCourseSwitcher

User script to enable quick switching of language courses on Duolingo.
24 stars 5 forks source link

Delete old 'dcs_courses' value. #7

Closed zeta12ti closed 7 years ago

zeta12ti commented 7 years ago

This is probably a Greasemonkey bug, but when you set a value with GM_setValue, the old value isn't deleted. The old values just pile up. If you check <firefox's folder>/gm_scripts/DuoLingo_Course_Switcher.db, you should see lots of extraneaous old values in the database.

This patch fixes this by deleting the old value just before setting the new value.

EDIT: It might be advisable to wrap the delete statement in an if statement to make sure 'dcs_courses' actually exists. I doubt it really causes any issues, but it will raise a warning if it doesn't exist.

Also, I'm wondering if it's possible to put the GM_setValue in a "finally" clause just in case there's an error or break before it gets set. I had this happen to me once and it cleared the old value without setting the new. It's probably pretty rare since it took weeks to happen to me and I'm refreshing the pages quite a bit since I'm porting this script to the new site.

EDIT2: Just committed all that. It should be more robust now.

arekolek commented 7 years ago

I'm unable to reproduce this...

Ubuntu 14.04 Firefox 53.0 Greasemonkey 3.8 Duolingo Course Switcher 0.8.2

$ sqlite3 gm_scripts/DuoLingo_Course_Switcher.db "select * from scriptvals;"
dcs_courses|"{\"en\":[{\"language\":\"it\",\"level\":23},{\"language\":\"es\",\"level\":22},{\"language\":\"fr\",\"level\":4},{\"language\":\"sv\",\"level\":1},{\"language\":\"uk\",\"level\":10}],\"es\":[{\"language\":\"pt\",\"level\":1},{\"language\":\"it\",\"level\":20},{\"language\":\"ca\",\"level\":1}],\"it\":[{\"language\":\"fr\",\"level\":1}],\"pl\":[{\"language\":\"en\",\"level\":1}]}"

After adding a new language on Duolingo:

$ sqlite3 gm_scripts/DuoLingo_Course_Switcher.db "select * from scriptvals;"
dcs_courses|"{\"en\":[{\"language\":\"it\",\"level\":23},{\"language\":\"es\",\"level\":22},{\"language\":\"fr\",\"level\":4},{\"language\":\"sv\",\"level\":1},{\"language\":\"uk\",\"level\":10}],\"es\":[{\"language\":\"pt\",\"level\":1},{\"language\":\"it\",\"level\":20},{\"language\":\"ca\",\"level\":1}],\"it\":[{\"language\":\"fr\",\"level\":1}],\"pl\":[{\"language\":\"en\",\"level\":1}],\"fr\":[{\"language\":\"it\",\"level\":1},{\"language\":\"es\",\"level\":1}]}"

To verify that the language has been added: https://www.diffchecker.com/FdapbdVS

But there are no extraneous old values in the database, there's only that one value.

What do you see in your database exactly?

zeta12ti commented 7 years ago

I'll see if I can reproduce it again. When I checked, there were hundreds of entries in the scriptvals table. I deleted them, but after a while they built up again. I did some testing and found that every time the script ran, a new entry was added. That's when I changed the script and it's worked for me for a while.

My attempt at reproduction:

Ubuntu 16.04 Firefox 53.0.3 (32-bit) Greasemonkey 3.11 Duolingo Course Switcher 0.8.2

First attempt: I'm getting "uncaught exception: unknown (can't convert to string)" at line 45 (where the GM_setValue is). Might be related to the fact that there were two copies of the database...

Try again: still can't get it to work. I'm going to try going to various parts of the site. I have the new website, so the basic page won't work for me.

Conclusion: can't reproduce. However, I can't get the script to work at all, so I'm not sure what's up. I had this same problem before and I can't remember how I fixed it. I think it was a cache issue. If I figure it out I'll post here. For the time being you can close this if you want. For the record, I'm pretty sure the script was working earlier today.

EDIT: Alright: I think I've figured this out. When you only have one course that DCS knows about, the GM_setValue seems to fail for some reason. Once it knew about two base languages, it worked as intended. When I tried it, I failed to reproduce the bug that I saw before. Instead, it looks like the old value is properly deleted (and the rowid is incremented). I suspect the database somehow got configured wrong before. I'll update if I see it again. For now, I'll definitely close this pull request.

Sorry for wasting your time with this.