How to change the default search engine with JavaScript?
-
Hello!
I'm modding Vivaldi and I wonder how to change the default search engine in Vivaldi with JavaScript code only. It's Bing, and I wish it was DuckDuckGo instead.
I've found this piece of code in background-common-bundle.js:"en-GB": { SEARCH_ENGINE_COLLECTION: { version: "3", default: "cj3wy7euf00633o5ubzefo8ug", defaultPrivate: "cj3wy7eug00663o5ub1v152yx", engines: [{ id: "cj3wy7euf00633o5ubzefo8ug", oldId: "1", name: "Bing", keyword: "b", url: "https://www.bing.com/search?FORM=INCOH2&PC=IFJ1&PTAG=ICO-c9d0fc87&q=%s", faviconUrl: a.b, suggestUrl: "https://www.bing.com/osjson.aspx?query=%s" }, { id: "cj3wy7euf00643o5ulxculg4b", oldId: "3", name: "Yahoo!", keyword: "y", url: "https://uk.search.yahoo.com/yhs/search?hspart=iry&hsimp=yhs-fullyhosted_009&type=dpp_vvldnu_00_00¶m1=1¶m2=pa%3Ddowncoll&p=%s", faviconUrl: a.l }, ...]} }
I think it is possible to change the default search engine using something like vivaldi.* (for instance I did
vivaldi.prefs.get("vivaldi.toolbars.navigation", function(a) { console.log(a) });
to get the order of the navigation toolbar icons).
However, I don't really see how to do that.
Do you have any idea?
Thank you. -
Why do you want to change it using JS rather than just doing it in Settings?
-
@sunazerty The local storage in use by Vivaldi is nothing but arrays and key value pairs, which you can manipulate with javascript. See backup search engines for an example. You will have to find the default engine, unset the value, find the engine of your choice and set it to default. Just iterate through the whole thing until you hit what you desire, then change it and ultimately save it back to storage.
-
@luetage Thank you. But since the Vivaldi default JS scripts are obfuscated, I can't find how to modify that object. I think it is possible using the Vivaldi API but I don't really see how to manage that preference.
-
@Komposten Because this is profile-dependant and I'd like to change the default search engine for every new profile in the browser.
-
@sunazerty Read up on manipulating local storage (e.g. https://developer.chrome.com/extensions/storage). You can check how Vivaldi set up the storage in dev tools, when you take a look at console and issue code like this:
chrome.storage.local.get({'SEARCH_ENGINE_COLLECTION': ''}, function(back) { const engines = back.SEARCH_ENGINE_COLLECTION; console.log(engines); });
No need to hunt down anything in the obfuscated code.
-
@luetage Thanks a lot! Everything works correctly when I try this:
chrome.storage.local.get('SEARCH_ENGINE_COLLECTION', function(a) { let result = e.SEARCH_ENGINE_COLLECTION; result.default = "cj3wy7etn003d3o5ulqefdv26"; chrome.storage.local.set({'SEARCH_ENGINE_COLLECTION': result}); });
-
@sunazerty In fact, this solution is also profile-dependent but a code snippet allows me to automate the change.
-
Ppafflick moved this topic from Modifications on