{"id":1554,"date":"2020-03-15T18:06:22","date_gmt":"2020-03-15T18:06:22","guid":{"rendered":"https:\/\/the-plot.com\/blog\/?p=1554"},"modified":"2020-03-16T10:42:22","modified_gmt":"2020-03-16T10:42:22","slug":"first-chrome-extension","status":"publish","type":"post","link":"https:\/\/the-plot.com\/blog\/?p=1554","title":{"rendered":"First chrome extension&#8230;"},"content":{"rendered":"\n<p>I&#8217;ve hacked together a quick [well, it took me all bloody afternoon, but it&#8217;s my first go] extension which does something pretty simple: when the button on the toolbar is pressed, it loops through all of the cookies for the domain [for the current tab], deletes them all and reloads the page.<\/p>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/13029458\/delete-cookies-chrome-extension\/13029554\">This<\/a> pointed me in the right direction, but I couldn&#8217;t figure out how [or for that matter why] to use the background.js to implement the delete function, so it&#8217;s called directly from the standard popup.js. <\/p>\n\n\n\n<p>There is one part of it which is not very reliable:<\/p>\n\n\n\n<p><code>var domain = new URL(tab.url).hostname;<br>\/\/ Almost certainly unreliable!<br>var domainNoDubs = domain.match(\/.*?([\\w]+.[\\w]+)$\/)[1];<\/code><\/p>\n\n\n\n<p>&#8230;which is basically an attempt to convert a domain <code>www.site.com<\/code> into <code>site.com<\/code>. There is a bunch of stuff that can go wrong here and my reliance on the &#8216;1th&#8217; item in the array returned is pretty flaky. But, having tested it exhaustively on 3 different sites [!], it works. The reason I need to do this is because this call..<\/p>\n\n\n\n<p><code>chrome.cookies.getAll({domain: domainNoDubs}, function(cookies) {...<\/code><\/p>\n\n\n\n<p>&#8230;requires the passing in of a domain prefixed with a dot. Simply replacing &#8216;<code>www<\/code>&#8216; with nothing isn&#8217;t good enough. What this parameter actually needs to be is the dot-prefixed second level domain &#8211; or the highest part of it if it has something in front of it. So for <code>www.this.site.com<\/code>, it would actually need to be <code>.site.com<\/code>.<\/p>\n\n\n\n<p>As I say, it works. More or less&#8230; Source is <a href=\"https:\/\/github.com\/zogspat\/zapNReload\">here<\/a>.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve hacked together a quick [well, it took me all bloody afternoon, but it&#8217;s my first go] extension which does something pretty simple: when the button on the toolbar is pressed, it loops through all of the cookies for the &hellip; <a href=\"https:\/\/the-plot.com\/blog\/?p=1554\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1554","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1554"}],"version-history":[{"count":3,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1554\/revisions"}],"predecessor-version":[{"id":1560,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1554\/revisions\/1560"}],"wp:attachment":[{"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-plot.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}