简体   繁体   中英

Chrome Extension to click on page load

I am trying to create a chrome extension that will click a link when the page of a certain site is loaded/refresh. So far I have not been able to get it to work, I've tried different code snippets from different sources but no matter what I try it still doesnt do anything.

My manifest looks like this --

 {
  "name": "SITENAME",
  "manifest_version": 2,
  "version": "1",
  "content_scripts": [
    {
      "matches": ["*://SITENAME.com/*"],
      "js": ["sitenamelink.js"]
    }
  ], "permissions": [
    "tabs" , "*://SITENAME.com/*"
  ]
}

The site will have random dynamic variables appended to the end of it, such as "sitename.com/product/model...etc. etc." so theres really no way for it to be predictable, only way would to find the page url and update the extension everytime which is not something I want to have to do.

I was trying to keep the js coding clean and simple and the js I have now is --

$(document).ready(function(){
$('#addToCartLink').trigger('click');
});

I also tried this --

jQuery.noConflict();
jQuery(document).ready(function() {
   jQuery('a#addToCartLink')[0].click();
});

and this --

$(document).ready(function(){
$('a#addToCartLink')[0].click();
});

The page that has the link, has it coded as such -- <a id="addToCartLink" href="javascript:addToCart()" onclick="showBubble(this)" onmouseout="hideBubble()"><span>Add to Cart</span></a>

Im not sure how to inspect it to see where my code is failing because when I inspect with chrome, it only shows the js errors from that page's coding.

So what am I doing wrong? Any help is much appreciated. Thanks.

Here's a test page, the size is selected if you follow this link, and now only requires for the add to cart link to be clicked. -- Test Page

My sample extension (see code below) worked fine for me.
Eg: Visiting the Test Page you provided, the ADD TO CART link is clicked and the item is added to cart.

manifest.json:

{
    "manifest_version": 2,

    "name":    "Test Extension",
    "version": "0.0",
    "offline_enabled": false,

    "content_scripts": [{
        "matches":    ["*://*.sitename.com/*"],
        "js":         ["content.js"],
        "run_at":     "document_end",
        "all_frames": false
    }]
}

content.js :

document.getElementById('addToCartLink').click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM