Bagaimanakah saya boleh melaksanakan skrip Greasemonkey beberapa kali pada halaman yang sama tanpa muat semula?

DDD
Lepaskan: 2024-11-15 03:20:02
asal
747 orang telah melayarinya

How can I execute a Greasemonkey script multiple times on the same page without a refresh?

Injecting Greasemonkey Script Multiple Times on the Same Page

For users new to Greasemonkey and JavaScript, a common requirement arises where a userscript needs to be executed multiple times without a page refresh. This scenario becomes essential when a request, such as an Amazon search, occurs using Ajax without triggering a complete page reload.

Solution: Utilizing waitForKeyElements() Utility

To address this need effectively, the waitForKeyElements() utility offers a robust and simple solution. This utility monitors a specified element and executes a callback function whenever the element's content changes. Below is a complete script that leverages jQuery and waitForKeyElements to modify Amazon search results:

// @name     _Amazon Search, alter results
// @include  http://www.amazon.com/s/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==

function addCustomSearchResult (jNode) {
    jNode.prepend (
        '<div>
Salin selepas log masuk

Implementation Details:

  • waitForKeyElements() Utility: This utility checks for the presence of an element with the ID "atfResults." When found, it executes the addCustomSearchResult() function.
  • addCustomSearchResult() Function: This function modifies the Amazon search results by injecting your custom content. In this example, it adds a custom div with the text "Buy my stuff, instead!" to the beginning of each result row.

Using this solution, you can now execute your Greasemonkey script multiple times on the same page without a refresh, providing you with greater flexibility and control over your browser interactions.

Atas ialah kandungan terperinci Bagaimanakah saya boleh melaksanakan skrip Greasemonkey beberapa kali pada halaman yang sama tanpa muat semula?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan