简体   繁体   English

如何修改用户提交的带有Chrome扩展程序的搜索输入中的查询?

[英]How can I modify the query from a user submitted search input with a Chrome extension?

So, I'm writing a Chrome extension for a school assignment and I'm stuck on this part where I think I need to parse a string. 因此,我正在编写用于学校作业的Chrome扩展程序,但在这一部分,我认为我需要解析字符串。 This is my first time working with HTML, CSS, and JS, so I'm just scouring the web for tips on how to make this happen. 这是我第一次使用HTML,CSS和JS,因此我只是在网上搜寻有关如何实现此目标的技巧。

Basically, in our assignment we need to create an extension for Chrome which essentially opens up a new page where we have a search bar. 基本上,在我们的任务中,我们需要为Chrome创建一个扩展程序,该扩展程序实际上会打开一个新页面,其中有一个搜索栏。 There's this website, https://www.manualslib.com/ , and the terms we input into that search bar need to be used to directly search that site. 有一个网站https://www.manualslib.com/ ,我们在搜索栏中输入的术语需要用于直接搜索该网站。

The problem is that this website's URL for searches is a bit different than what I'm used to: when you search for something, the website takes the first letter of my search term, puts it in between slashes, and then adds at the end the whole search. 问题在于该网站的搜索URL与我惯用的URL有所不同:当您搜索某项内容时,该网站会以我搜索词的首字母开头,将其放在斜杠之间,然后在末尾添加整个搜索。 For example, if you search genetics there, the URL will look like https://www.manualslib.com/g/genetics.html . 例如,如果您在此处搜索genetics ,则URL看起来像https://www.manualslib.com/g/genetics.html

So I guess I need to parse the string somehow to get the search term's first character, right? 因此,我想我需要以某种方式解析字符串以获取搜索字词的第一个字符,对吗? According to w3schools, using the split() method would be a good idea. 根据w3schools的说法,使用split()方法将是一个好主意。 The problem is I'm not really sure how the syntax works here. 问题是我不太确定语法在这里如何工作。 As I said, it's my first time dabbing in web developing, so I'm not familiar at all with the way people do stuff here. 正如我所说的,这是我第一次从事Web开发,所以我对人们在这里做事的方式完全不熟悉。 Any help is appreciated. 任何帮助表示赞赏。

This is the code I have for the search bar (this code merely redirects to the webpage, it doesn't search anything): 这是我用于搜索栏的代码(此代码仅重定向到网页,不搜索任何内容):

 <div id="tfheader"> <form id="tfnewsearch" method="get" action="https://www.manualslib.com/"> <input type="text" class="tftextinput" name="str" ; size="21" maxlength="120"> <input type="submit" value="search" class="tfbutton"> </form> </div> 

What kind of changes do I have to make? 我必须进行哪些更改? Do I need to create a .js file where I write the function that parses the string? 我需要在编写用于解析字符串的函数的地方创建一个.js文件吗? Or is the parsing function written inside the HTML file? 还是解析功能写在HTML文件中? Any help is appreciated, thanks in advance! 任何帮助表示赞赏,在此先感谢!

 var query = 'genetics'; var url = 'https://www.manualslib.com/{0}/{1}.html' .replace('{0}', query.substring(0, 1)) .replace('{1}', query); console.log(url); 

The onsubmit event might help you. onsubmit事件可能会对您有所帮助。

To redirect to another site, just set window.location , like this: 要重定向到另一个站点,只需设置window.location ,如下所示:

search_form.onsubmit = function() // will be called as soon as the form is submitted
{
    var searchText = typehead.value; // the text entered in the search bar
    var redirectTo = "https://www.manualslib.com/"+searchText[0]+"/"+searchText+".html";
    window.location = redirectTo;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在chrome扩展程序中保存用户输入? - How can I save user input in my chrome extension? 如何从chrome扩展程序中打开标签页,等待用户输入并返回扩展程序? - how do I, from chrome extension, open tab wait for user input and go back to extension? 在Chrome扩展程序中,如何从Chrome浏览器中准确检索用户的区域/区域/国家/地区? - In a Chrome Extension, how can I accurately retrieve the user's region/locale/country from their Chrome browser? chrome-extension:等待用户输入 - chrome-extension: wait for input from user Chrome扩展程序可以修改用户的主机文件吗? - Can a Chrome Extension modify a user's host file? 如何使用用户输入修改数组(在 html 中与表格一起显示) - How can I modify array with user input (displayed with table in html) 如何根据 Chrome 扩展中的用户选项修改 http 请求标头? - How to modify http request headers based on user options in a Chrome extension? 如何收集用户提交的照片? - How can I collect a photo submitted by a user? 如何拦截和修改来自Chrome扩展程序的HTTP身份验证请求? - How do I intercept and modify HTTP Auth requests from a Chrome extension? 如何修改订阅的搜索查询? - How do I modify the search query on a subscription?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM