1
我尝试在Twitter的.com网页上发起点击事件。但是,当我在我的扩展中尝试代码时,它不起作用。在twitter chrome扩展中启动点击事件
if(typeof TIP == 'undefined'){
var TIP = {};
}
TIP.Tweets = {
getNumberOfTweets: function(){
var tweets = $('#stream-items-id .js-stream-item').length;
return tweets;
},
setNumberOfTweets: function(number){
TIP.Tweets.currentNumberOfTweets = number;
}
}
TIP.initiate = function(){
TIP.Tweets.currentNumberOfTweet = TIP.Tweets.getNumberOfTweets();
setInterval(function(){
var tweets = TIP.Tweets.getNumberOfTweets();
if(TIP.Tweets.currentNumberOfTweets != tweets){
TIP.Tweets.setNumberOfTweets(tweets);
TIP.renderImages();
}
}, 100);
//Render images
TIP.renderImages();
}
TIP.renderImages = function(){
$('#stream-items-id .js-view-details').each(function(){
var btn = this
if($.trim($(this).text()) == 'View photo'){
$(btn).click();//This is the button I want to click, which is the view photo button
console.log('rendered');
}
});
}
$(function(){
TIP.initiate();
});
但是,当我尝试手动插入代码它的作品。我在Google Chrome的控制台中插入以下代码:
$('#stream-items-id .js-view-details').each(function(){
var btn = this
if($.trim($(this).text()) == 'View photo'){
$(btn).click();
console.log('rendered');
}
});
如果我尝试做其他事情而不是发起点击事件。它工作正常。例如,如果我想操纵按钮的内部html。它在扩展代码中工作正常。只有当我尝试在扩展中启动点击时,才会出现此问题。
请求manifest.json档案:
{
"name": "Twitter Image Previewer",
"version": "1.0",
"description": "Enables image previews in tweets",
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": ["http://twitter.com/*", "https://twitter.com/*"],
"css": ["style.css"],
"js": ["jquery.min.js", "JeasyCreate.js", "extension.js"],
"all_frames": true
}
],
"permissions": [
"http://*/",
"https://*/"
]
}
我可以自己回答我的问题。我认为通过扩展程序启动点击事件在Google Chrome中被禁止,因为安全原因 – einstein 2012-03-25 12:31:49
此代码是如何执行的?你能显示'manifest.json'吗? – 2012-03-25 13:20:05
Rob W检查上面的manifest.json。我更新了它。 – einstein 2012-03-25 13:22:59