Note that there are some explanatory texts on larger screens.

plurals
  1. POChrome Extension not running everytime page action button is clicked
    primarykey
    data
    text
    <p>I am creating an extension that I need to have the ability to run the content script multiple times when clicking on the page action button. I have this in my background.js:</p> <pre><code>chrome.pageAction.onClicked.addListener(function(tab) { alert('calling content_script'); chrome.tabs.executeScript(null, { file: 'content_script.js' },function(){alert("success");}); }); </code></pre> <p>This works the first time the button is clicked. When clicked a second time, I get my popup saying "calling content_script" but the content script is never executed. Why is this?</p> <p>Here is the background script in its entirety:</p> <pre><code>function checkForValidUrl(tabId, ChangeInfo, tab){ if(tab.url.indexOf("tiger.armstrong")&gt; -1){ chrome.pageAction.show(tabId); if(tab.url.indexOf("tiger.armstrong") == 0){ chrome.pageAction.hide(tabId); } } } chrome.tabs.onUpdated.addListener(checkForValidUrl); chrome.pageAction.onClicked.addListener(function(tab) { alert('calling content_script'); chrome.tabs.executeScript(null, { file: 'content_script.js' },function(){alert("success");}); }); </code></pre> <p>Here is the manifest:</p> <pre><code>{ "name": "LiveLab Post Grades", "version": "2.0", "permissions": [ "activeTab","tabs","http://*/*","https://*/*" ], "background": { "scripts": ["jquery.min.js","background3.js"], "persistent": false }, "page_action": { "default_icon": { "19": "GIcon.png" }, "default_title": "LiveLab Tools" }, "content_scripts": [ { "js": [ "jquery.min.js" ], "matches": [ "http://*/*", "https://*/*"], "run_at": "document_end" }], "manifest_version": 2 } </code></pre> <p>Here is the content script:</p> <pre><code>var livelabtools = { /** * this function is like window.open, but it can POST (rather than GET) from js * source: http://canop.org/blog/?p=426 */ canop_open: function (verb, url, data, target) { var form = document.createElement("form"); form.action = url; form.method = verb; form.target = target || "_self"; if (data) { //for (var key in data) { var input = document.createElement("input"); input.name = 'data'; input.value = data;//typeof data[key] === "object" ? JSON.stringify(data[key]) : data[key]; form.appendChild(input); //console.log(form); //} } // these two lines are only needed for ie //form.style.display = 'none'; //document.body.appendChild(form); form.submit(); console.log("form submit === " + form); form.remove(); }, post_grades: function () { alert('in post grades!!!!'); var str, exercise, i = 0; grades = {}; do { ex_str = "form1:tabSet1:tabInstr:lp2:tabSet4:tabp:lpProgress:ts1:tab7:lp7:table3:rg3:" + i + ":tc3:st3"; lname_str = "form1:tabSet1:tabInstr:lp2:tabSet4:tabp:lpProgress:ts1:tab7:lp7:table3:rg3:" + i + ":tc1:st1"; grade_str = "form1:tabSet1:tabInstr:lp2:tabSet4:tabp:lpProgress:ts1:tab7:lp7:table3:rg3:" + i + ":tc7:st7_field"; exercise = document.getElementById(ex_str); lname = document.getElementById(lname_str); grade = document.getElementById(grade_str); if (exercise != null) { if (grades[lname.innerHTML] === undefined) grades[lname.innerHTML] = {}; console.log(lname.innerHTML + ", " + exercise.innerHTML + ", " + grade.innerHTML); if (grade.value != null &amp;&amp; grade.value != '') grades[lname.innerHTML][exercise.innerHTML] = grade.value; else grades[lname.innerHTML][exercise.innerHTML] = "0"; } i++; } while (exercise != null); // console.log(JSON.stringify(grades)); // console.log(JSON.stringify(grades).length) //window.open("http://aspen2.cscofc.info/jsontocsv.php?data="+JSON.stringify(grades)); console.log('posting...' + "\n JSON.String... = "+ JSON.stringify(grades)); livelabtools.canop_open("post", "http://aspen2.cscofc.info/jsontocsv.php", JSON.stringify(grades)); console.log('done'); return "function end"; } } console.log(livelabtools.post_grades()); </code></pre> <p>I won't go into detail about it, unless asked but the important parts to note are the return statement and the console log. Everything runs perfectly fine the first time the page action button is clicked, and when finished, I get "function end" printed to the console. After the initial run, however, whenever I click on the page action button, I get an alert saying "calling content_script" and nothing else happens. Why won't my content script run more than once?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload