honestbleeps / Reddit-Enhancement-Suite

Reddit Enhancement Suite
http://redditenhancementsuite.com
GNU General Public License v3.0
4.13k stars 881 forks source link

Add omnibar suggestion for subreddits #4900

Open sbordeyne opened 5 years ago

sbordeyne commented 5 years ago

I coded a chrome extension for something that always bothered me, typing "reddit.com/r/" in the omnibar. This is what goes into the background.js file.

// This event is fired with the user accepts the input in the omnibox.
var callback = function(text){
    // add reddit.com/r/ to the URL and encode user subreddit for special characters , / ? : @ & = + $ #
    var newURL = 'http://reddit.com/r/' + encodeURI(text);
    chrome.storage.local.get("tabURL", function(item){
        chrome.tabs.query({active: true}, function(tab){
            //get the active tab, if it's a new tab, open the link in that, otherwise
            //create a new tab to open in
            if (item.tabURL.url.length === 0){
                chrome.tabs.update(tab.id, {url: newURL, active:true});
            }
            else{
                chrome.tabs.create({ url: newURL });
            }
        });
    });
}

chrome.tabs.onActivated.addListener(function (tabInfo){
    chrome.storage.local.set"tabURL:chrome.tabs.get(tabInfo.tabId).url);
});

chrome.omnibox.onInputEntered.addListener(callback);

chrome.omnibox.onInputChanged.addListener(
  function(text, suggest) {
    suggest([
      {content: text + "/top/?sort=top&t=all", description: text + "/top/ (all time)"},//all time top posts
      {content: text + "/controversial/?sort=top&t=all", description: text + "/controversial/ (all time)"},// controversial
      {content: text + "/new/", description: text + "/new/"}// new posts
    ]);
  });

This is the manifest.json file:

  {
    "name": "r/ Reddit Shortcut",
    "version": "1.2",
    "omnibox": { "keyword" : "r" },
    "background": {
      "persistent": false,
      "scripts": ["background.js"]
    },
    "description": "Omnibar enhancement for reddit. Type r <TAB> <subreddit_name> to go to the sub, or select a suggestion",
    "manifest_version": 2,
    "permissions": ["tabs", "storage"]
  }
jewel-andraia commented 5 years ago

This is neat, but might a little overkill since Chrome lets you create custom search engines. That said, the top/controversial/new suggestions are a nice value-add, as is adding a preconfigured tool for many uses' benefit. Since most of RES' target browsers support chrome.omnibox now, this would probably be a good candidate -- assuming that the feature would first check if it were enabled inside RES' user settings.

A few recommendations:

  1. When creating issues/PRs, write a more substantial headline like "Add omnibar suggestion for subreddits".
  2. Replace http://reddit.com with https://www.reddit.com to avoid redirects.
  3. Investigate a more robust technique for detecting "is the active tab a 'new tab'?" This could easily fail on websites with that same title, or if the user has selected a different UI language.
  4. For the manifest.json description, you don't need to mention "An extension to..." -- that's obvious from the context.
sbordeyne commented 5 years ago

Thanks for the input, I've edited the code in my issue.

There's not really a good way to check if the tab is a new tab. From what I know though, the new tab page is titled "New Tab" even though the rest of chrome is in another language.