javascript - Chrome Extension Replace word in current textarea -



javascript - Chrome Extension Replace word in current textarea -

i'm trying create chrome extension replaces lastly word typed in current <textarea> when user makes keydown event fire. i've tried doesn't work.

here files of extension:

my extension's manifest.json:

{ "name": "test", "description": "test", "version": "0.0.1", "permissions": [ "activetab" ], "background": { "scripts": ["background.js"], "persistent": false }, "browser_action": { "default_title": "replace test" }, "manifest_version": 2 }

its background.js:

chrome.tabs.executescript(null, {file: "content_script.js"});

and content_script.js:

document.onkeydown = replaceprevword; // azerty: ctrl + ² // qwerty: ctrl + ' function keypress(e) { var evtobj = window.event? event : e if (evtobj.keycode == 222 && evtobj.ctrlkey) homecoming true; } function getcaretposition(ctrl) { var caretpos = 0; // ie back upwards if (document.selection) { ctrl.focus(); var sel = document.selection.createrange(); sel.movestart('character', -ctrl.value.length); caretpos = sel.text.length; } // firefox back upwards else if (ctrl.selectionstart || ctrl.selectionstart == '0') caretpos = ctrl.selectionstart; homecoming (caretpos); } function returnword(text, caretpos) { var index = text.indexof(caretpos); var pretext = text.substring(0, caretpos); if (pretext.indexof(" ") > 0) { var words = pretext.split(" "); homecoming words[words.length - 1]; //return lastly word } else { homecoming pretext; } } function replaceprevword() { if(keypress()){ var text = document.activeelement; var caretpos = getcaretposition(text) var word = returnword(text.value, caretpos); if (word != null) { text.value = text.value.replace(word,"replaced"); } } }

this works fine on jsfiddle (http://jsfiddle.net/cyo646cr/3/), don't know how in chrome extension.

any ideas?

thanks.

issue

you not injecting script correctly. it's easy say, because calling simple line

chrome.tabs.executescript(null, {file: "content_script.js"});

in background.js result in injecting script one time , in current tab (because no tabid specified). hence your script run 1 time every session of google chrome in first tab open.

take @ the documentation chrome.tabs.executescript.

work around

to work around problem you've got 2 options:

declare "content_scripts" field in manifest.json injected in page matches pattern. add listener chrome.tabs.onupdated.addlistener method , inject script on tab. option 1

declare "content_scripts" field in manifest.json this:

... "content_scripts": [ { "matches": ["<all_urls>"], "js": ["content_script.js"], "run_at": "document_idle", "all_frames": true } ], ...

now content script injected in every page (because "<all_urls>" match pages) , in frames of pages. can find helpful informations "content_scripts" here.

option 2

request permissions chrome.tabs api , urls want in manifest.json:

... "permissions": [ "activetab", "tabs", "<all_urls>" ] ...

in background.js, add together listener chrome.tabs.onupdated, fire on every tab update (i.e. url update), , inject script using chrome.tabs.executescript, this:

chrome.tabs.onupdated.addlistener(function(tabid, info, tab) { chrome.tabs.executescript(tabid, {file: "content_script.js"}); });

see the documentation chrome.tabs.onupdated.

javascript jquery html google-chrome google-chrome-extension

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -