2013-09-22 47 views
-1

當用戶單擊並按住Chrome擴展中的任何頁面時,如何觸發某個功能? 這裏是我使用的是什麼和它不工作:用戶點擊並按住觸發功能[鍍鉻擴展]

manifest.json的

{ 
    "manifest_version": 2, 

    "name": "Harvix extension", 
    "description": "Shortcut to search on Harvix", 
    "version": "0.0.1", 

    "content_scripts": [ 
     { 
      "matches": ["http://*/*"], 
      "js": ["jquery.js", "core.js"] 
     } 
    ], 

    "permissions": [ 
     "tabs", "<all_urls>" 
    ] 
} 

core.js

var timeout; 
function open(text) { 
    $('#myElement').mousedown(function() { 
     timeout = setTimeout(null, 1000); 
    }).bind('mouseup mouseleave', function() { 
     clearTimeout(timeout); 
    }); 
    alert('hello'); 
} 

chrome.browserAction.onClicked.addListener(function(tab) { 
    chrome.tabs.executeScript(null, {code: open(window.getSelection().toString())}); 
}); 
+0

可能重複的[觸發點擊並保留事件](http://stackoverflow.com/questions/14445375/trigger-click-and-hold-event) –

+0

我認爲這個問題是關於darg&放在jquery – Pixeladed

+0

如果你選擇不在你的擴展中使用jQuery,那麼同樣的方法適用,但你仍然需要推出自己的Click和Drag方法,雖然我收集了''js「:[」jquery.js「,」core「。你的代碼中的js「]行意味着你可以使用jQuery庫來使你的生活更輕鬆,不妨使用它 –

回答

0

在你core.js,替換此:

chrome.browserAction.onClicked.addListener(function(tab) { 
    chrome.tabs.executeScript(null, {code: open(window.getSelection().toString())}); 
}); 

有了這個:

$('body').on('click', '*', function() { 

    open(window.getSelection().toString());   

}); 

這將克服不能夠對象的內容腳本中chrome.browserAction對象的問題。