background.js 874 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. chrome.storage.sync.get('user_css', function(val) {
  2. console.log('got sync', val);
  3. if (typeof val.user_css != 'undefined') {
  4. localStorage['user_css'] = val.user_css;
  5. }
  6. });
  7. chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  8. if (request.method == "getCSS") {
  9. var href = request.href;
  10. console.log('requested styles for', href);
  11. var user_css = localStorage['user_css'];
  12. var lines = user_css.split("\n");
  13. var tmp = [];
  14. var section = "";
  15. for (var i = 0; i < lines.length; i++) {
  16. var line = lines[i];
  17. if (line.indexOf("##") == 0) {
  18. section = line.substr(2).trim();
  19. continue;
  20. }
  21. if (section == "" || section == "GLOBAL" || href.indexOf(section) != -1)
  22. tmp.push(line);
  23. }
  24. sendResponse({user_css: tmp.join("\n")});
  25. } else {
  26. console.warn('unknown method', request.method);
  27. sendResponse({});
  28. }
  29. });