乐闻世界logo
搜索文章和话题

How to set text in the google search box using Tampermonkey?

1个答案

1

When using Tampermonkey, you can automate the process of filling specific text into the Google search box by writing a JavaScript script. This feature is useful for various purposes, such as testing or quickly searching for fixed keywords.

Step 1: Create a New Script

First, create a new user script in the Tampermonkey extension. Click on the browser extension icon and select 'Add New Script'.

Step 2: Write the Script

Next, write the following JavaScript code in the script editor:

javascript
// ==UserScript== // @name Google Search Autofill // @namespace http://tampermonkey.net/ // @version 0.1 // @description try to take over the world! // @author You // @match https://www.google.com/* // @grant none // ==/UserScript== (function() { 'use strict'; // Listen for page load completion window.addEventListener('load', function() { // Select the Google search box var searchInput = document.querySelector('input[name="q"]'); // Check if the search box exists if (searchInput) { // Set the search box value searchInput.value = "Tampermonkey"; } }); })();

This script's primary function is to locate the Google search input box (with name attribute typically "q") after the page loads and set its value to "Tampermonkey".

Step 3: Save and Test

After saving the script, open a new Google search page, and you should see the search box automatically filled with "Tampermonkey".

Important Notes

  • Ensure the script runs on the correct URL (as indicated by the @match tag in the code above).
  • Given that Google may update its page structure, if the script stops working unexpectedly, you may need to check and update the selectors or other logic.

By doing this, you can modify the script as needed to automatically set different text in the Google search box or perform other operations.

2024年7月21日 20:42 回复

你的答案