0

Sorry for my poor English, i hope you can understand the issue.

I'm new to chrome extension development,and for sure in my code there are a lot of thing to change or optimize; anyway i've written a simple code that, (seems) works at least from my chrome.

The code clicks a button every X minutes in specific page, then wait and parse the result in page.

I've :

  • a content script (loaded from manifest.json) which "inject" some button and text Input box in page, so user can sets some "filter params" before click a "start button"; the start button then sendMessage() to background.js to set Alarm Event for the click ;

  • an eventPage (which is set persistent true in actually ) which handle the request from tabs and set a countdown alarm for each tab; when X min are passed fire a message to the interested tab;

I also have a popup.html e popup.js not important here (i think).

I've to distribuite this extension manually, so i would distribuite a zip that user can load with "developer mode ".

*Now the issue is: why the code was working only on my Chrome ? *

I've tested with others 2-3 laptop with Chrome, the background script is loaded (i can see the background page printint console log)

but in webpage the contents.js seems no way executed .

In my chrome works well: i can see in console some initial output (i print the name of dir extension to check) and the dynamic created element (button,input box ect.) in page.

And all is working, i can fire the start button and receive results of parsing.

During the development i've never run the extension on other machine. Yesterday i've succssfully tested on 2-3 laptop.. then i made only few change but nothing serious.

Today i can run only in my chrome.

In other pc nothing, neither the simple console.log output first line of script.

I can read in console log : "Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist." but this also in my working istance in my laptop chrome .

The zip file is the same and the extraction is good, in fact i can actually load the extension and i see the background page debug console.log() sentences

In some case, in laptop where it dosen't work, i've received a message relative jQuery and the fact that chrome.runtime.sendMessage() is not defined; and it points to code in webpage, not mine.

I've see that in webpage code there is something like:

 var extid = "mcmhdskbnejjjdjsdkksmeadjaibo";
          var extVer = "1.5";
          var extStatus = 0;
   $(document).ready(function () { 
///...
  chrome.runtime.sendMessage(extid, {message: "version"},
                  function (reply) {
                      if (reply) {
                          if (reply.version) {
                              if (reply.version == extVer) {
                                  if (reply.gminfo != 'OK') {
                                      extStatus = 1; /// ...

Seems that chrome.runtime is undefined, and the webpage can't call the sendMessage(). EDIT: this undefined occurs only when my extension is loaded

Maybe there is some conflict when i load my extension? But in my chrome browser works...

Can some expert indicate in where direction i've to investigate?

Thanks a lot for any suggestions.

My Manifest.json :

  {"manifest_version": 2,
  "name": "myAlarm",
  "description": "This extension alerts.",
  "version": "0.1",
   "permissions": [
   "alarms",
   "system.cpu",
   "storage",
   "tabs",
    "webNavigation",
   "https://www.mytargetsite.com/subUrl/"
     ],

   "web_accessible_resources": [
   "icon.png",
   "vanillaSelectBox.css"],

    "content_scripts": [
    {
    "matches": ["https://www.mytargetsite.com/subUrl/"],
     "css": ["vanillaSelectBox.css"],
     "js": ["jquery-3.3.1.min.js","vanillaSelectBox.js","taffy-min.js","content.js"],
     "run_at": "document_end"
     }
      ],

    "background": {
    "scripts": ["eventPage.js"],
    "persistent": true
     },

     "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
      },

   "icons": {
     ....
      }
      }

My contents,js (stripped):

     chrome.runtime.onMessage.addListener(
            function(request, sender) {
          // here i parse message "time'up" from background js    
              });

    window.addEventListener('load', function() {
     var pt=chrome.runtime.getURL('filterOff.wav'); 
      var p=pt.split("/");

      console.log("[myAlarm v0.1] started" );
      console.log("[myAlarm v0.1] folder : ("+p[2]+")");
       // here i start an active wait for the presence in page of button with ID= btntarget_id
      waitForElementToDisplay("#btntarget_id", 500); //when function find button then create and add button and input text to webpage
         });

My eventPage.js :

            var curr_alarms =[];
             chrome.extension.onMessage.addListener(function(request, sender)
              {   /// here receive start countdown message from content.js and set alarm  ...
               }


            chrome.alarms.onAlarm.addListener(function(alarm) {
              /// here i manage each alarm for each tab
              });

              chrome.tabs.onRemoved.addListener(function(tabid, removed) { 
                //  ...
             });

            chrome.tabs.onUpdated.addListener(function
             (tabId, changeInfo, tab) {
               //
               });

edit : in browser where it dosen't work i can read also :

Access to XMLHttpRequest at 'https://mytargetsite.com/suburl/grid.php' (redirected from 'https://mytargetsite.com/suburl/grid.php') from origin 'https://mytargetsite.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

1 Answers1

0
  1. The fact that the declared content script runs or not, should be verified by inspecting in devtools => sources sub-tab => content scripts sub-sub-tab. If it really doesn't run, there can be just two explanations: the URL is different (for example not https) or extensions are blocked by their domain admin via runtime_blocked_hosts which you can see in chrome://policy.

  2. Your development mode extension's id will be different on a different machine unless you pin it by adding a "key" in manifest.json

  3. To use chrome.runtime to send messages to your extension from a webpage code (not from a content script!) your extension's manifest should declare "externally_connectable" and use a different event onMessageExternal, see also sending messages from web pages.

  4. The CORS error may be irrelevant to your code (you can investigate the source of the error by expanding the error's call stack in devtools console).

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • 1- You're right ! I've spent some hours and then discovered than a 302 redirect url did some mismatch in loading ! So this is the real issue. 2- I catch the moment : i'm a bit confused... adding key is stricly need to distribuite "manually" my unpacked extension ? (i mean, i would send a zip file to users; then they extract and load.. is it ok ? or i'm missing something?) – NotARealCoder Dec 17 '19 at 17:39