0

im trying to install a little chrome extension for test-purposes. The function works fine, but not in the extension itself. I think the problem is the background.js . im not sure which part of the code must be in it. Hope anyone can help me out.

Here´s the codes so far. Manifest:

{
  "name": "Getting Started Example",
  "version": "1.0",
  "description": "Build an Extension!",
  "permissions": ["activeTab", "declarativeContent", "storage"],
  "options_page": "options.html",
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "images/16x16.png",
      "19": "images/19x19.png",
      "38": "images/38x38.png",
      "128": "images/128x128.png"
    }
  },
  "icons": {
    "16": "images/16x16.png",
    "19": "images/19x19.png",
    "38": "images/38x38.png",
    "128": "images/128x128.png"
  },
  "manifest_version": 2
}

popup.html:

<!DOCTYPE html>
<html>
  <head>
    <style>
      button {
        height: 30px;
        width: 30px;
        outline: none;
      }
    </style>
  </head>
  <body>
        <span id="params"></span>
    <pre id="results">{}</pre>
    <script src="popup.js"></script>
  </body>
</html>

popup.js:

 function getAllUrlParams(url) {
  var queryString = url ? url.split('?')[1] : window.location.search.slice(1);
  var obj = {};

  if (queryString) {
    queryString = queryString.split('#')[0];
    var arr = queryString.split('&');

    for (var i = 0; i < arr.length; i++) {
      var a = arr[i].split('=');
      var paramName = a[0];
      var paramValue = typeof (a[1]) === 'undefined' ? true : a[1];

      if (paramName.match(/\[(\d+)?\]$/)) {
        var key = paramName.replace(/\[(\d+)?\]/, '');
        if (!obj[key]) obj[key] = [];

        if (paramName.match(/\[\d+\]$/)) {
          var index = /\[(\d+)\]/.exec(paramName)[1];
          obj[key][index] = paramValue;
        } else {
          obj[key].push(paramValue);
        }
      } else {
        if (!obj[paramName]) {
          obj[paramName] = paramValue;
        } else if (obj[paramName] && typeof obj[paramName] === 'string'){
          obj[paramName] = [obj[paramName]];
          obj[paramName].push(paramValue);
        } else {
          obj[paramName].push(paramValue);
        }
      }
    }
  }

  return obj;
}

var params = document.getElementById('params');
var results = document.getElementById('results');

url=window.location.href; 
params.innerText =url;  
results.innerText = JSON.stringify(getAllUrlParams(url), null, 2);

and last, but not least the background.js(thats only the code from the starter package, nothing changed cause i do not know exactly what to post inside):

  chrome.runtime.onInstalled.addListener(function() {
  chrome.storage.sync.set({color: '#3aa757'}, function() {
    console.log('The color is green.');
  });
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    chrome.declarativeContent.onPageChanged.addRules([{
      conditions: [new chrome.declarativeContent.PageStateMatcher({
        pageUrl: {hostEquals: 'developer.chrome.com'},
      })],
      actions: [new chrome.declarativeContent.ShowPageAction()]
    }]);
  });
});
Nubba
  • 1
  • 1
  • What error you are getting, open the background page in developer tools, and check the console. – Ace Jul 17 '20 at 09:51
  • 1) The current code in the background script doesn't do anything meaningful, it's just a copypasta of the demo extension, moreover it tries to use page_action but your extension is using browser_action. You don't need the background script at all. 2) `location.href` won't do you no good in the popup, it'll be the URL of the popup page, not the web page. See [How can I get the current tab URL for chrome extension?](https://stackoverflow.com/a/32530054) – wOxxOm Jul 17 '20 at 09:54

0 Answers0