Question

I am trying to trigger a click artificially via content script. While the same code works and triggers the click when tried from console, it doesn't when tried from contentscript.

If I try to echo the selected element on which I am applying the click , I got this :-

Via Console

[<a class=​"blue apply-coupon">​APPLY COUPON​</a>​]

SO, the link is properly selected here

Via Contentscript , I got the link as some object like this

[a.blue.apply-coupon, prevObject: e.fn.e.init[1], context: document, selector: ".coupon-section a", constructor: function, init: function…] 

I tried to trigger the click by both .click() and by making a click even and triggering :-

1. $('.coupon-section').find('a').click();

2. var e = jQuery.Event( "click" );
jQuery('.coupon-section a').trigger(e);

None of them working. Can anyone suggest what wrong am I doing and is there any way to trigger when the link is available as an object as it is available in case of contentscripts.

Update : On expanding the link object, I get this. I am adding the object only upto click.

0: a.blue.apply-coupon
context: document
length: 1
prevObject: e.fn.e.init[1]
0: div.coupon-section
context: document
length: 1
prevObject: e.fn.e.init[1]
selector: ".coupon-section"
__proto__: Object[0]
_toggle: function (a){var b=arguments,c=a.guid||f.guid++,d=0,e=function(c){var e=(f._data(this,"lastToggle"+a.guid)||0)%d;f._data(this,"lastToggle"+a.guid,e+1),c.preventDefault();return b[e].apply(this,arguments)||!1};e.guid=c;while(d<b.length)b[d++].guid=c;return this.click(e)}
add: function (a,b){var c=typeof a=="string"?f(a,b):f.makeArray(a&&a.nodeType?[a]:a),d=f.merge(this.get(),c);return this.pushStack(S(c[0])||S(d[0])?d:f.unique(d))}
addClass: function (a){var b,c,d,e,g,h,i;if(f.isFunction(a))return this.each(function(b){f(this).addClass(a.call(this,b,this.className))});if(a&&typeof a=="string"){b=a.split(p);for(c=0,d=this.length;c<d;c++){e=this[c];if(e.nodeType===1)if(!e.className&&b.length===1)e.className=a;else{g=" "+e.className+" ";for(h=0,i=b.length;h<i;h++)~g.indexOf(" "+b[h]+" ")||(g+=b[h]+" ");e.className=f.trim(g)}}}return this}
after: function (){if(this[0]&&this[0].parentNode)return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this.nextSibling)});if(arguments.length){var a=this.pushStack(this,"after",arguments);a.push.apply(a,f.clean(arguments));return a}}
ajaxComplete: function (a){return this.on(b,a)}
ajaxError: function (a){return this.on(b,a)}
ajaxSend: function (a){return this.on(b,a)}
ajaxStart: function (a){return this.on(b,a)}
ajaxStop: function (a){return this.on(b,a)}
ajaxSuccess: function (a){return this.on(b,a)}
andSelf: function (){return this.add(this.prevObject)}
animate: function (a,b,c,d){function g(){e.queue===!1&&f._mark(this);var b=f.extend({},e),c=this.nodeType===1,d=c&&f(this).is(":hidden"),g,h,i,j,k,l,m,n,o;b.animatedProperties={};for(i in a){g=f.camelCase(i),i!==g&&(a[g]=a[i],delete a[i]),h=a[g],f.isArray(h)?(b.animatedProperties[g]=h[1],h=a[g]=h[0]):b.animatedProperties[g]=b.specialEasing&&b.specialEasing[g]||b.easing||"swing";if(h==="hide"&&d||h==="show"&&!d)return b.complete.call(this);c&&(g==="height"||g==="width")&&(b.overflow=[this.style.overflow,this.style.overflowX,this.style.overflowY],f.css(this,"display")==="inline"&&f.css(this,"float")==="none"&&(!f.support.inlineBlockNeedsLayout||cv(this.nodeName)==="inline"?this.style.display="inline-block":this.style.zoom=1))}b.overflow!=null&&(this.style.overflow="hidden");for(i in a)j=new f.fx(this,b,i),h=a[i],cn.test(h)?(o=f._data(this,"toggle"+i)||(h==="toggle"?d?"show":"hide":0),o?(f._data(this,"toggle"+i,o==="show"?"hide":"show"),j[o]()):j[h]()):(k=co.exec(h),l=j.cur(),k?(m=parseFloat(k[2]),n=k[3]||(f.cssNumber[i]?"":"px"),n!=="px"&&(f.style(this,i,(m||1)+n),l=(m||1)/j.cur()*l,f.style(this,i,l+n)),k[1]&&(m=(k[1]==="-="?-1:1)*m+l),j.custom(l,m,n)):j.custom(l,h,""));return!0}var e=f.speed(b,c,d);if(f.isEmptyObject(a))return this.each(e.complete,[!1]);a=f.extend({},a);return e.queue===!1?this.each(g):this.queue(e.queue,g)}
append: function (){return this.domManip(arguments,!0,function(a){this.nodeType===1&&this.appendChild(a)})}
appendTo: function (c){var d=[],e=f(c),g=this.length===1&&this[0].parentNode;if(g&&g.nodeType===11&&g.childNodes.length===1&&e.length===1){e[b](this[0]);return this}for(var h=0,i=e.length;h<i;h++){var j=(h>0?this.clone(!0):this).get();f(e[h])[b](j),d=d.concat(j)}return this.pushStack(d,a,e.selector)}
attr: function (a,b){return f.access(this,a,b,!0,f.attr)}
before: function (){if(this[0]&&this[0].parentNode)return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this)});if(arguments.length){var a=f.clean(arguments);a.push.apply(a,this.toArray());return this.pushStack(a,"before",arguments)}}
bind: function (a,b,c){return this.on(a,null,b,c)}
blur: function (a,c){c==null&&(c=a,a=null);return arguments.length>0?this.on(b,null,a,c):this.trigger(b)}
change: function (a,c){c==null&&(c=a,a=null);return arguments.length>0?this.on(b,null,a,c):this.trigger(b)}
children: function (c,d){var e=f.map(this,b,c);L.test(a)||(d=c),d&&typeof d=="string"&&(e=f.filter(d,e)),e=this.length>1&&!R[a]?f.unique(e):e,(this.length>1||N.test(d))&&M.test(a)&&(e=e.reverse());return this.pushStack(e,a,P.call(arguments).join(","))}
clearQueue: function (a){return this.queue(a||"fx",[])}
click: function (a,c){c==null&&(c=a,a=null);return arguments.length>0?this.on(b,null,a,c):this.trigger(b)}

Please comment if I am unclear in asking my question.

Thanks !

Était-ce utile?

La solution

Solved this problem temporarily by using javascript :-

document.getElementsByClassName('apply-coupon')[0].click();

Still would love to know why that was not getting clicked via jQuery !

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top