3

I am writing a plugin to process useful information for myself inside the Amazon seller central dashboard.

For some reason a click() call on a Next page link will not fire from inside the context script.

The code is running, but this does nothing:

var $next = $('a:contains(Next)');
$next.click();

but strangely this works:

var $next = $('a:contains(Next)');
window.location = $next.attr("href");

The example of the HTML of the link is:

<td align="right" valign="middle" width="60%" class="tiny">
    <nobr>&nbsp;&nbsp;&nbsp;<a href="https://sellercentral-europe.amazon.com/gp/feedback-manager/view-all-feedback.html?ie=UTF8&amp;currentPage=2&amp;dateRange=&amp;descendingOrder=1&amp;pageSize=50&amp;sortType=Date">Next</a>
    </nobr>
</td>

Are there any restrictions on running click events inside Chrome extension? A button press on another page works just fine (although that one is structured differently - see below):

<a class="buttonImage" name="View all your feedback" href="https://sellercentral-europe.amazon.com/gp/feedback-manager/view-all-feedback.html/ref=fb_fbmgr_vwallfb?ie=UTF8&amp;dateRange=&amp;descendingOrder=1&amp;sortType=Date">
    <span class="awesomeButton buttonLarge primaryLargeButton inner_button">
        <span class="button_label">View all your feedback</span>
    </span>
</a>

1 Answer 1

6

DOM Elements have a native .click() method. Therefore the following examples work:

$('a')[0].click() (example)

$('a').get(0).click() (example)

document.querySelector('a').click() (example) - it's worth pointing out that jQuery isn't required

Relevant W3 documentation.


Which leads me to assume that you can't programmatically change the URL when selecting a collection of elements (a jQuery object). I guess you need to select the individual DOM element. Therefore, the following should work:

var $next = $('a:contains(Next)');
$next[0].click();
Sign up to request clarification or add additional context in comments.

6 Comments

This is a more useful answer for me. For completeness I would like to confirm that all browsers have a click() method on DOM elements though (although for my extension only Chrome matters of course).
@TrueBlueAussie I presume $next[0].click() worked? Here is relevant documentation: developer.mozilla.org/en-US/docs/Web/API/HTMLElement.click
This works perfectly in a live situation. Many thanks for the references :) Very odd that it does not translate to a real click in jQuery.
But why does jquery click work in the console and not in the extension? (I've got the same problem)
@Daniel - Are you using the same code in your console and extension? Double check that the code is being called in your extension... you may have to ask a question and include the relevant code?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.