nileshtrivedi / better

Browser extension that suggests better/ethical/local/cheaper alternative products & services than your current tab's URL.
MIT License
89 stars 6 forks source link

Destination URLs should be templates to populate named captures from url pattern #12

Open nileshtrivedi opened 4 years ago

nileshtrivedi commented 4 years ago

Destination URLs should be able to take params from the current URL. For eg: if you are visiting a particular book on Amazon.com whose ISBN can be picked from the URL, the same ISBN can be passed on to the alternatives like OpenLibrary. This can be done by using named captures in the regexp which seems to be supported in Javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges

This will be useful for directly taking the user to exactly the right destination from a large set (products, books, research papers, or anything that comes from a large set and has a somewhat portable identifier).

Dynamic templating in pure JS can be achieved with a helper function like this:

const templateString = "Hello ${this.name}!";
const templateVars = {
    name: "world"    
}

const fillTemplate = function(templateString, templateVars){
    return new Function("return `"+templateString +"`;").call(templateVars);
}

console.log(fillTemplate(templateString, templateVars));
nileshtrivedi commented 4 years ago

This will cover majority of the use-cases where portable identifiers are part of the URL. This will only miss those cases where the identifier like ISBN cannot be captured from the URL, but can be found in page content somewhere.

deepaktatineni commented 4 years ago

I can pick this up.

nileshtrivedi commented 4 years ago

@deepaktatineni Great! Discuss here if needed, and send a PR!