JavaScript Google Chrome extention that adds a feature to search highlighted text on specific webpages from a custom context menu

此生再无相见时 提交于 2021-01-29 08:06:36

问题


I am trying to write the Chrome extension, when you right-click on the word(email) it will search it on custom fields on a list of webpages, in new tabs.

Tried to copy Simple=Select+Search extension, but it requires adding %s in the search link, instead of the search item and some of my links does not change after you press "search" Like here

This is the function that does the search the function generates the actual search URL combining the selected text with the search engine config

fixes stuff like & in the search string plus vs %20 replace %s with a search string

function createURL (idSE, info) {
    var selectedText = encodeURIComponent(info.selectionText);
    if (config.searchEngines[idSE].plus)
        selectedText = selectedText.replace(/%20/g, "+");
    return config.searchEngines[idSE].url.replace(/%s/g, selectedText).replace(/%S/g, selectedText);
}

// standard search function
function genericSearch (info, tab, idSE) {
    var urlSE = createURL(idSE, info);
    if (config.searchEngines[idSE].incognito) {
        chrome.windows.create({
            "url": urlSE,
            "incognito": true
        });
    } else if (config.newTab) {
       
        searchOnNewTab(urlSE, tab);
    } else {
        chrome.tabs.update(tab.id, {
            "url": urlSE
        });
    }
    trackGA(idSE);
}

So my question is, how can I specify the search field on the webpage(by Xpath or selector, maybe) to input my search item there?

All that I need is: Highlight item > Right-Click > Choose webpage where I need to search that email at > New tab opens with that webpage search result

Don`t need to post it on Google Web Store, it is for my use only.

Using Javascript. Created a base for the extension(manifest.json, icon, some HTML files), but could not create a working prototype that will open a new tab and paste the highlighted item in the preferred field.

Some clarification: I have an email address and a number of admin consoles where I need to paste and search that email. I'm writing an extension where I can highlight that email, right-click on it and the context menu should show a list of those admin consoles (console1, console2, console3), clicking on which should open that console in a new tab (website) and automatically paste and search for the records associated with that email address.

来源:https://stackoverflow.com/questions/65366216/javascript-google-chrome-extention-that-adds-a-feature-to-search-highlighted-tex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!