james-fray / tab-reloader

Multiple-browser tab reloader extension
https://webextension.org/listing/tab-reloader.html
112 stars 31 forks source link

Feature request: Run js code and change detection #139

Closed kohanla closed 1 year ago

kohanla commented 1 year ago

With "run js code" it is possible to transform the content of the tab (for example remove some HTML). Unfortunately "play sound on change" seems to calculate the hash of the page before "run js" which causes the extension to play sound on every load. So it would be great to have an option that told the extension to detect change after "run js".

Ok, this exactly the same text as 138, but in the other issue I got a bit sidetracked because of that pop-up sound issue ...

You wrote, you were not able to reproduce this. Probably because my wording was not clear enough. So let me explain..

If you reload a page that has a timer (for example timeanddate.com) then a "play sound on change" is always triggered because the timer changes frequently.

So let's say you are not interested in the timer and eliminate it from the page with "Run JS Code", then the reloader will still trigger a change because the hash calculation happens before "Run JS Code".

Here is a simple JavaScript example for "Run JS Code": document.getElementsByTagName("body")[0].innerText='nochange'

If the code execution happened after the hash calculation, then reloading would not trigger any "play sound on change" because the output of my JavaScript example above never changes.

Hope I didn't miss anything :-)

kohanla commented 1 year ago

This what I tried. In your test 0.6.0 build I moved the hash calculation into a .then() after the 'code-value' part. Like so

          args: [id, profile['code-value']]
        })).then(() =>  api.inject(tabId, {
          files: ['/data/scripts/sha.js']
        })).catch(error);

It seems to work for my purpose. Obviously a dirty hack (maybe not even correct). Having an option would be nicer of course, hence my feature request.

james-fray commented 1 year ago

We need to have it before the user script to make sure the content is untouched. For your case, use the custom HASH function of FAQ19.

kohanla commented 1 year ago

I accept this answer because "custom hash" could work. Having said that ...

We need to have it before the user script to make sure the content is untouched.

It is not clear who you mean by "we". But I don't understand why the content has to be untouched? I just described a use case where it is useful to calculate the hash after content changed (Run JS). An additional option where the user can change the order would solve this problem easily.

The custom hash feature could work, but it would be a lot of boilerplate code in addition to the one or two lines of code I use to adjust content from a page.

Also the example in the FAQ 19 has to be adjusted, right? Because it seems that it would not work with multiple tabs? (See also #137).

Thanks again for your extension and also for fixing my other two issues so quickly.

james-fray commented 1 year ago

The code in the FAQ is now updated to consider per-page keys. Thanks! https://webextension.org/listing/tab-reloader.html?rev

Also, note that you can set the localStorage variable tab-reloader-element to ask the hash function to calculate the hash of an element instead of the body (to find the static part of the page with dynamic elements). See https://github.com/james-fray/tab-reloader/blob/master/v3/data/scripts/sha.js

For more complex matchings, you need to have your embedded HASH function.

kohanla commented 1 year ago

Thanks for changing the FAQ page.